目标:否 .third类中的p标签上的红色轮廓。下面过滤出来的父母的jQuery后代仍在收藏中
自给例子,或这里的jsfiddle:http://jsfiddle.net/WJVBm/
拼命地寻找着授予绿色的对勾...在此先感谢您的帮助!
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var myDivs = $('div');
var myFilteredDivs = myDivs.filter(function(){
return $(this).closest('.third').length == 0;
});
var myFilteredParagraphs = myFilteredDivs.find('p'); // why does this find paragraphs in divs that have already been filtered out of the collection?
myDivs.css('border', '1px solid #CCC');
myFilteredDivs.css('border', '1px solid blue');
myFilteredParagraphs.css('border', '1px solid red'); // paragraphs inside of divs that should not exist in the collection are still being outlined by a red border
});
</script>
<style type="text/css">
div { float: left; padding: 20px; margin: 5px; }
</style>
</head>
<body>
<div class="first">
<p>first</p>
<div class="second">
<p>second</p>
<div class="third">
<p>third</p>
</div>
</div>
<div class="second">
<p>second2</p>
</div>
<div class="third">
<p>third2</p>
</div>
</div>
</body>
</html>
这是因为它们也是其他非过滤元素的后代。根据你的实际标记,你可能会用'children()'而不是'find()'离开。 – 2012-08-15 14:45:32
问题是'myDivs'包含每个比赛中的每个div和它的嵌套子节点。 添加'console.log(myDivs);',你可以在输出中看到它。 – Nope 2012-08-15 14:47:07