我正在使用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');
答案 0 :(得分:1)
您可以通过用,
分隔多个元素来选择它们。
$('#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');
答案 1 :(得分:0)
var foundElems = $();
$('#some-container .some-class').each( function(index) {
//filter $(this)
//store $(this)
foundElems=$.merge(foundElems,$(this));
});
$('#some-container2 .some-class2').each( function(index) {
//filter $(this)
//store $(this)
foundElems=$.merge(foundElems,$(this));
});
//do this again for many containers....
//then call some jQuery function on each element of that collection
foundElems.fadeOut('slow');
您还可以将所有选择器查询与逗号合并:
var foundElems = $();
$('#some-container .some-class,#some-container .some-class2,#some-container .some-class3').each( function(index) {
//filter $(this)
//store $(this)
foundElems=$.merge(foundElems,$(this));
});
//then call some jQuery function on each element of that collection
foundElems.fadeOut('slow');
答案 2 :(得分:0)
你的jQuery看起来很好,在你的.each语句结束时你应该有一个元素数组。您的问题是您无法在数组对象上调用.fadeOut。
你需要这样做:
$.each(foundElems, function() {
$(this).fadeOut("slow");
});
答案 3 :(得分:0)
我能想到的最简单的解决方案:
var array;
array = $.merge($(),$.find('#someContainer .someClass, #someContainer2 .someClass2'));
array.fadeOut(1000);