- 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 :only-child 選擇器
jQuery 中的:only-child 選擇器用於選擇其父元素的唯一子元素。換句話說,它目標是那些在相同父元素中沒有兄弟姐妹的元素。
語法
以下是 jQuery :only-child 選擇器的語法:
$(":only-child")
引數
此選擇器將篩選並選擇作為其父元素唯一子元素的元素。
示例 1
在下面的示例中,我們使用 jQuery :only-child 選擇器來選擇每個作為其父 <div> 唯一子元素的 <p> 元素:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p:only-child").css("background-color", "yellow");
});
</script>
</head>
<body>
<div style="border: 1px solid black;">
<p>Only paragraph</p>
</div><br>
<div style="border: 1px solid black;">
<p>First paragraph</p>
<p>Second paragraph</p>
</div><br>
<div style="border: 1px solid black;">
<p>Only paragraph</p>
</div>
</body>
</html>
執行上述程式後,選定的元素將以黃色背景色突出顯示。
示例 2
在這裡,我們選擇每個作為其父元素 (<div>) 唯一子元素的 <span> 元素:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
// Add a class to the only child of each parent div
$("div span:only-child").css("background-color", "yellow");
});
</script>
</head>
<body>
<div style="border: 1px solid black;">
<span>Only span</span>
</div><br>
<div style="border: 1px solid black;">
<span>First span</span>
<span>Second span</span>
</div><br>
<div style="border: 1px solid black;">
<span>Another span</span>
<span>And another one</span>
</div>
</body>
</html>
執行後,選定的元素將以黃色背景色突出顯示。
jquery_ref_selectors.htm
廣告
