我知道我很近,但需要在这里提示!我试图根据特定父级是否在类名中添加标签,从内部包含其他元素的某些类元素中选择数据。
<div class=something_1>
<div class=other>
<div class=other>
<div class=other_TEMPLATE_1>
<div class=other>
<div class=selected_thing>I DO NOT WANT THIS</div>
</div>
</div>
</div>
</div>
</div>
<div class=something_2>
<div class=other>
<div class=other>
<div class=other_TEMPLATE_2>
<div class=other>
<div class=selected_thing>I DO NOT WANT THIS</div>
</div>
</div>
</div>
</div>
</div>
<div class=something_3>
<div class=other>
<div class=other>
<div class=other>
<div class=other>
<div class=selected_thing>I WANT THIS</div>
</div>
</div>
</div>
</div>
</div>
<script>
$("[class^=something]").each(function(){
selectedThing = $(this).children().children().children(":not[class^=other_TEMPLATE]").children().children(".selected_thing").html();
console.log(selectedThing);
});
</script>
正如您所看到的,部分挑战是某些类名称可能略有改变,但我想忽略TEMPLATE出现的那些。
答案 0 :(得分:2)
你可以用这个:
$('.selected_thing').each(function(){
if($(this).parents('[class*="TEMPLATE"]').length==0){
//do something
$(this).css('color','red')
}
})
<强> jsFiddle example 强>
答案 1 :(得分:1)
$("[class^=something]").each(function(){
selectedThing = $(this).find('div').not("[class^=other_TEMPLATE]").children().children(".selected_thing").html();
console.log(selectedThing);
});
答案 2 :(得分:1)
var selectedThings = [];
$("[class^=something]").each(function(){
selected = $(this).find('div')
.not("[class^=other_TEMPLATE]")
.children().children(".selected_thing")
.html();
if(selected !== undefined)
{
selectedThings.push(selected);
}
});
console.log(selectedThings);
编辑:放慢速度:( @nevermind赢了......
答案 3 :(得分:1)
$('.selected_thing').not('[class*="TEMPLATE"] .selected_thing');