- jQuery 教程
- jQuery - 首頁
- jQuery - 路線圖
- jQuery - 概述
- jQuery - 基礎
- jQuery - 語法
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 屬性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 新增元素
- jQuery - 刪除元素
- jQuery - 替換元素
- jQuery CSS 操作
- jQuery - CSS 類
- jQuery - 尺寸
- jQuery - CSS 屬性
- jQuery 效果
- jQuery - 效果
- jQuery - 動畫
- jQuery - 鏈式操作
- jQuery - 回撥函式
- jQuery 遍歷
- jQuery - 遍歷
- jQuery - 遍歷祖先元素
- jQuery - 遍歷子孫元素
- jQuery UI
- jQuery - 互動
- jQuery - 小部件
- jQuery - 主題
- jQuery 參考
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍歷
- jQuery - 其他
- jQuery - 屬性
- jQuery - 實用工具
- jQuery 外掛
- jQuery - 外掛
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用資源
- jQuery - 問答
- jQuery - 快速指南
- jQuery - 有用資源
- jQuery - 討論
jQuery filter() 方法
jQuery 中的filter方法用於篩選符合特定條件的元素。
此方法允許您根據特定條件(例如特定的 CSS 選擇器、評估每個元素的函式或包含要匹配的元素的 jQuery 物件)來細化一組選定的元素。不符合指定條件的元素將從選擇中刪除,而符合條件的元素將被返回。
filter() 方法與 not() 方法的作用相反。
語法
以下是 jQuery 中 filter() 方法的語法:
$(selector).filter(criteria,function(index))
引數
此方法接受以下可選引數:
criteria (可選):可以是選擇器表示式、jQuery 物件或一個或多個元素,也可以是新增到現有元素組的 HTML 片段。
function(index) (可選):用於對集合中每個元素執行的函式。此函式應返回 true 以包含元素,返回 false 以排除元素。
示例 1
在下面的示例中,我們使用 jQuery 的 filter() 方法根據 CSS 選擇器篩選特定元素:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('li').filter('.highlight').css('color', 'green');
});
</script>
</head>
<body>
<ul>
<li class="highlight">Item 1</li>
<li>Item 2</li>
<li class="highlight">Item 3</li>
<li>Item 4</li>
</ul>
</body>
</html>
執行上述程式後,類為 "highlight" 的 <li> 元素的顏色將變為綠色。
示例 2
在這個示例中,我們演示如何篩選所有類為 "highlight1" 和 id 為 "highlight2" 的 <li> 元素:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('li').filter('.highlight1, #highlight2').css('color', 'green');
});
</script>
</head>
<body>
<ul>
<li class="highlight1">Item 1</li>
<li>Item 2</li>
<li id="highlight2">Item 3</li>
<li>Item 4</li>
</ul>
</body>
</html>
執行上述程式後,類為 "highlight1" 和 id 為 "highlight2" 的 <li> 元素將被篩選,顏色為“綠色”。
示例 3
下面的程式使用包含類為 ".other-divs" 的元素的 jQuery 物件 "$otherDivs" 篩選所有 <div> 元素:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// Defining a jQuery object containing elements to match against
const $otherDivs = $('.other-divs');
// Filtering <div> elements using the defined jQuery object
$('div').filter($otherDivs).css('border', '2px solid green');
});
</script>
</head>
<body>
<div class="other-divs">I'm Mickey Mouse.</div>
<div>I'm goofy.</div>
<div class="other-divs">I'm Donald Duck.</div>
<div>I'm Pluto.</div>
</body>
</html>
執行上述程式後,類為 "other-divs" 的 <div> 元素將具有綠色的邊框。
示例 4
在下面的示例中,我們演示瞭如何使用 filter() 方法和一個函式來篩選 <div> 元素,依據是它們是否具有類 'highlight':
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('div').filter(function() {
return $(this).hasClass('highlight');
}).css('background-color', 'green');
});
</script>
</head>
<body>
<div class="highlight">I'm Mickey Mouse.</div>
<div>I'm goofy.</div>
<div class="highlight">I'm Donald Duck.</div>
<div>I'm Pluto.</div>
</body>
</html>
執行上述程式後,類為 "highlight" 的 <div> 元素的背景顏色將變為綠色。
