.filter(selector) 此方法用于在匹配元素中按照选择器表达式进行筛选。 记住:使用此方法必须得传入选择器表达式参数,不然会报错“’nodeType’ 为空或不是对象” 另外请注意这个filter方法和jquery中的find方法的区别: filter方法是对匹配元素进行筛选,而find方法是对匹配元素的后代元素进行筛选。
例子:
<body>
<div class="main">
<P class="p1">这是第一段</P>
<P class="p2">这是第二段</P>
</div>
</body>
--------------------------
find用法:
<script type="text/javascript">
$(document).ready(function(){
//alert($(".main").filter(":not(:has(.p1)").text());
var text=$(".main").find(".p1").text();
alert(text);
});
</script>
-----------
filter的用法:
<script type="text/javascript">
$(document).ready(function(){
//alert($(".main").filter(":not(:has(.p1)").text());
var text=$("p").filter(".p1").text();
alert(text);
});
</script>