filter(fn )
优质
小牛编辑
127浏览
2023-12-01
描述 (Description)
filter( fn )方法过滤与指定函数不匹配的匹配元素集中的所有元素。
语法 (Syntax)
以下是使用此方法的简单语法 -
<i>selector</i>.filter( selector )
参数 (Parameters)
以下是此方法使用的所有参数的说明 -
fn - 使用与$.each类似的当前元素$.each 。 如果函数返回false,则删除元素,否则保留元素。
例子 (Example)
以下示例显示了此方法的简单用法 -
<html>
<head>
<title>The jQuery Example</title>
<script type = "text/javascript"
src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("li").filter(function (index) {
return index == 1 || $(this).attr("class") == "middle";
}).addClass("selected");
});
</script>
<style>
.selected { color:red; }
</style>
</head>
<body>
<div>
<ul>
<li class = "top">list item 1</li>
<li class = "top">list item 2</li>
<li class = "middle">list item 3</li>
<li class = "middle">list item 4</li>
<li class = "bottom">list item 5</li>
<li class = "bottom">list item 6</li>
</ul>
</div>
</body>
</html>
例子 (Example)
以下示例显示了此方法的简单用法 -
<html>
<head>
<title>The jQuery Example</title>
<script type = "text/javascript"
src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("li").filter(function (index) {
return index == 1 || $(this).attr("class") == "middle";
}).addClass("selected");
});
</script>
<style>
.selected { color:red; }
</style>
</head>
<body>
<div>
<ul>
<li class = "top">list item 1</li>
<li class = "selected">list item 2</li>
<li class = "selected">list item 3</li>
<li class = "selected">list item 4</li>
<li class = "bottom">list item 5</li>
<li class = "bottom">list item 6</li>
</ul>
</div>
</body>
</html>