我正在使用 jQuery 运行 HTML 文件以收集元素。然后,我想对找到的每个元素执行 jQuery 操作。这是我到目前为止所拥有的,但它不起作用,因为我不知道如何存储每个方法中的 jQuery 元素。有人可以帮忙吗?
var foundElems = [];
$('#some-container .some-class').each( function(index) {
//filter $(this)
//store $(this)
foundElems.push($(this));
});
$('#some-container2 .some-class2').each( function(index) {
//filter $(this)
//store $(this)
foundElems.push($(this));
});
//do this again for many containers....
//then call some jQuery function on each element of that collection
foundElems.fadeOut('slow');
最佳答案
您可以通过用 ,
分隔来选择多个元素。
$('#some-container .some-class, #some-container2 .some-class2').fadeOut('slow');
如果您必须容纳一堆这些元素,您可以使用选择器构建一个数组,并将该连接数组传递到 jQuery 函数中。
var arr = ["#some-container2 .some-class2","#some-container .some-class"];
$(arr.join(",")).fadeOut('slow');
关于jquery - 将 jQuery 元素收集到集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16057005/