Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery选择具有类A、类B或类C的元素_Javascript_Jquery_Arrays_Class_Jquery Selectors - Fatal编程技术网

Javascript jQuery选择具有类A、类B或类C的元素

Javascript jQuery选择具有类A、类B或类C的元素,javascript,jquery,arrays,class,jquery-selectors,Javascript,Jquery,Arrays,Class,Jquery Selectors,我正在做一些需要两个函数的事情 1-我需要查看同一父级下的一组子级,并根据类名“active”,将该元素的ID添加到数组中 ['foo-a','foo-b','foo-d'] 2-然后,我遍历另一个父元素中的所有子元素,对于每个元素,我想知道它是否有与数组中的ID匹配的类名 这个元素有foo-a、foo-b或foo-d类吗?总是有.is('.foo-a、.foo-b、.foo-d')。或者,如果您实际上只是想选择它们,而不是对每个元素进行迭代和决定,$('.foo-a、.foo-b、.foo-

我正在做一些需要两个函数的事情

1-我需要查看同一父级下的一组子级,并根据类名“active”,将该元素的ID添加到数组中

['foo-a','foo-b','foo-d']
2-然后,我遍历另一个父元素中的所有子元素,对于每个元素,我想知道它是否有与数组中的ID匹配的类名


这个元素有foo-a、foo-b或foo-d类吗?

总是有
.is('.foo-a、.foo-b、.foo-d')
。或者,如果您实际上只是想选择它们,而不是对每个元素进行迭代和决定,
$('.foo-a、.foo-b、.foo-d',startingPoint)
对于第一部分,我会使用和:

这将为您提供一个id值数组(例如,
['foo-a','foo-d']
)。然后,您可以使用以下命令创建一个选择器,如
.foo-a、.foo-b、.foo-c
(the):

这将生成有效的jQuery选择器字符串,例如
'.foo-a、.foo-d'
。然后,您可以使用此选择器查找所需的元素,方法是:

然后,您可以使用
activeEls
执行任何需要的操作

var active = $("#foo").find(".active").map(function() {
    return this.id;
}).get();

$("#anotherParent *").each(function() {
   var that = this;
   var classes = $(this).attr("class");
   if(classes.indexOf(" ") !== -1) {
       classes = classes.split(" ");
   } else {
       classes = [ classes ];  
   }
   $.each(classes, function(i, val) {
       if($.inArray(val, active)) {

           // this element has one of 'em, do something with it
           $(that).hide();
       }
   });
});
var activeSelector = '.' + activeGroups.join(', .');
var activeEls = $('#secondParent').find(activeSelector);
var active = $("#foo").find(".active").map(function() {
    return this.id;
}).get();

$("#anotherParent *").each(function() {
   var that = this;
   var classes = $(this).attr("class");
   if(classes.indexOf(" ") !== -1) {
       classes = classes.split(" ");
   } else {
       classes = [ classes ];  
   }
   $.each(classes, function(i, val) {
       if($.inArray(val, active)) {

           // this element has one of 'em, do something with it
           $(that).hide();
       }
   });
});