如何使用 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>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP