- 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 #id 選擇器
jQuery 中的 #id 選擇器用於選擇具有特定 id 屬性的元素。id 屬性在文件中必須是唯一的,確保 #id 選擇器只針對一個唯一的元素。
選擇器區分大小寫,這意味著 "#MyID" 和 "#myid" 如果都存在,則會選擇不同的元素。
語法
以下是 jQuery 中 #id 選擇器的語法:
$("#id")
引數
id 選擇器以 "#" 開頭,後跟要選擇的元素的 id 值。
示例 1
在下面的示例中,我們使用 jquery #id 選擇器來更改 <div> 元素中文字的顏色:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#demo").css("background-color", "yellow");
})
});
</script>
</head>
<body>
<div id="demo">This text will turn blue.</div>
<button>Click</button>
</body>
</html>
當我們執行上述程式時,id 為 "demo" 的元素會將其文字背景顏色更改為黃色。
示例 2
在這個示例中,我們正在更改 <p> 元素的文字內容:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#demo").text("The text has been changed!");
})
});
</script>
</head>
<body>
<p id="demo">Hello mate, click the button below.</p>
<button>Click</button>
</body>
</html>
當我們點選按鈕時,id 為 "demo" 的元素會將其文字內容更改為 "The text has been changed!"。
示例 3
這裡,我們使用 jQuery #id 選擇器來隱藏一個 <p> 元素:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#demo").hide();
})
});
</script>
</head>
<body>
<p id="demo">This paragraph will be hidden.</p>
<button>Click</button>
</body>
</html>
點選按鈕後,id 為 "demo" 的元素使用 hide() 方法隱藏它。
jquery_ref_selectors.htm
廣告
