filter(fn)
将匹配元素集合中不满足‘fn函数’匹配的元素集合删除,形成新的元素集合
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("div").css("background", "#b4b0da")
.filter(function (index) {
return index == 1 || $(this).attr("id") == "fourth";
})
.css("border", "3px double red");
});
</script>
<style>
div { width:60px; height:60px; margin:5px; float:left;
border:3px white solid; }
</style>
</head>
<body>
<div id="first"></div>
<div id="second"></div>
<div id="third"></div>
<div id="fourth"></div>
<div id="fifth"></div>
<div id="sixth"></div>
</body>
</html>
.filter(function (index) {
return index == 1 || $(this).attr("id") == "fourth";
})
将div元素集合中索引为1和id等于‘fourth’以外的div元素都删除,形成新的div元素集合。以下是新的div元素集合
<div id="second"></div>
<div id="fourth"></div>