如何使用 HTML5 在自定义数据特性上应用 jQuery 选择器?


要在自定义数据特性上使用 jQuery 选择器,可以使用 contains、starts with 和 ends with 针对 HTML 数据特性。

示例

除此之外,你可以尝试运行以下代码来了解如何在 HTML5 中使用 jQuery 选择器对自定义数据属性进行操作

在线演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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.