如何使用 HTML5 在自定義資料特性上應用 jQuery 選擇器?


要在自定義資料特性上使用 jQuery 選擇器,可以使用 contains、starts with 和 ends with 針對 HTML 資料特性。

示例

除此之外,你可以嘗試執行以下程式碼來了解如何在 HTML5 中使用 jQuery 選擇器對自定義資料屬性進行操作

線上演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

  //stored selector
  var group = $('ol[data-group="Subjects"]');

  // starts with M
  var maths = $('[data-subject^="M"]',group).css('color','green');

  // contains the string graph
  var graph = $('[data-subject*="graph"]',group).css('color','blue');

});
</script>
</head>
<body>

<ol data-group="Subjects">
  <li data-subject="Maths">Maths</li>
  <li data-subject="Science">Science</li>
  <li data-subject="Geography">Geography</li>
  <li data-subject="History">History</li>
</ol>

</body>
</html>

更新於: 2019-12-18

瀏覽 293 次

啟動你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.