我有一组'div'标签,如下所示,
<div id="filtersrow" style="display:inline-block;margin-left:13px">
<button id="subfilterrow1" type="button" class="subclassrow1 active"></button>
<button id="subfilterrow2" type="button" class="subclassrow2"></button>
<button id="subfilterrow3" type="button" class="subclassrow3 active"></button>
</div>
<div id="filtercol" style="display:inline-block;margin-left:13px">
<button id="subcol1" type="button" class="subcol1 active"></button>
<button id="subcol2" type="button" class="subcol2" style="height:28px;transform:rotate(-90deg);width:25px;"></button>
<button id="subcol3" type="button" class="subcol3 active"></button>
</div>
<div id="morefilter" style="display:inline-block;margin-left:13px">
<button id="more1" type="button" class="moreclass1"></button>
<button id="more2" type="button" class="moreclass1 active"></button>
</div>
每个“按钮”标签中都有三组'div'标签。我的目的是通过每个div和检查按钮标签类有“活动”。如果它有活动,那么我想在数组中推送该类。我试过但无法解决这个问题。请帮我。谢谢
答案 0 :(得分:0)
使用此代码:
var arr = [];
$('div > button.active').each(function (ele) {
arr.push(ele);
});
答案 1 :(得分:0)
您可以使用map来迭代所有active
个类,将className拆分为数组&amp;删除活动类:
var classes = $("button.active").map(function() {
// Split the classnames (could use classList if available)
var split = this.className.split(/\s+/);
// Remove the active class, we already know it has it
// Could remove this check too?
var activeClass = split.indexOf("active");
if(~activeClass) split.splice(activeClass);
return split;
}).get();
console.log(classes);
答案 2 :(得分:0)
您可以使用.map()
,
var buttons = $("div button.active").map(function() {
return $(this).attr("id");
});
console.log(buttons);
此代码将使用类名active
选择div中的所有按钮,并将它们推入数组中。
答案 3 :(得分:0)
试试这个
var res = [];
$("div button.active").each(function() {
var className = $(this).attr("class").split(" ")[0];
res.push(className);
});
console.log(res);
答案 4 :(得分:0)
var ids = $("div button.active").map(function() {
return $(this).attr("id");
}).get();
alert(ids);
演示: