2

There are some ways to get all CSS styles of a DOM Element that are set with style or class Attribute. This works great, but if the class is not set to this it's not possible to find the classes that could be set to this element.

Now I'm looking for a way to get all CSS Classes and Styles that could be possible set to this DOM Element. I've tried something with RegExp but it's not perfect because I'm not sure how to search the document Style Node and compare it with the DOM Node. How is that possible?

Example what i need:

<div class="set">
  <p class="anotherclass">some text</p>
  <p class="otherClass">some text</p>
</div>

the css:

div.set { /* some css */ }
div.notSet { /* you could find me but not want to set me */ }
.autoSet_findMeToSet { /* set me to all tags with jquery */ }
div.autoSet_setMeNow { /* set me to the div tag with jquery */ }
div > p:first-child.autoSet_yeah { /* set me to the first p tag with jquery */
/* and so on */

As you can see I want to set with jQuery all css classes with special names. With regexp I could find the class names but I don't know how to check to which dom element (html tag) it needs to be set...

thanks for help at all!

4

2 回答 2

0

如果我理解正确的问题:

$(".class *")

选择具有类的元素内的所有元素class

$(".class").find(element).length === 0

检查元素是否在具有类的元素内class

于 2013-04-14T11:42:41.483 回答
0

您可以使用 jQuery $.contains() 检查找到的元素是否在其他元素中

更多信息在这里如何做到这一点:http: //api.jquery.com/jQuery.contains/

因此,对于您的示例,要查找某些找到的类是否在集合内,您可以执行以下操作:

$.each(foundElement, function(index, domElement){
    if($.contains(domElement, $('.set'))) {
        // domElement is inside the container with 'set' class
    } else {
        // domElement is not inside the container with 'set' class
    }
});
于 2013-04-14T11:13:00.037 回答