如何編寫 jQuery 選擇器來查詢 href 屬性中帶有 # 的連結?
範例
你可以嘗試執行以下程式碼,編寫一個 jQuery 選擇器來查詢 href 中帶有 # 的連結。這裡,^ 用於查詢 href 中以 # 開頭的連結。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('a[href^="#"]').click(function(){
alert('Success! Selected link starting with # in href.');
});
});
</script>
</head>
<body>
<a href="#demo" title="new" >Demo</a><br>
<a href="/cplusplus/" title="C++ Tutorial" >C++</a>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統
作業系統
java
iOS
HTMN
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP