什么时候应该使用数据属性?
自定义数据属性旨在存储页面或应用程序私有的自定义数据,没有更合适的属性或元素。
这次用data属性来表示通知气泡的气泡值。
<a href="#" class="pink" data-bubble="2">Profile</a>
此时间用于显示工具提示的文本。
<a href="#" class="tooltip" data-tip="this is the tip!">This is the link</a>
什么时候不应该使用数据属性?
我们不应该将数据属性用于任何已经建立或更合适的属性。例如不适合使用:
<span data-time="20:00">8pm<span>
当我们可以在如下时间元素中使用已经定义的datetime 属性时:
<time datetime="20:00">8pm</time>
通过 CSS 使用数据属性 (Attribute selectors)
[data-role="page"] {
/* Styles */
}
在 jQuery 中使用数据属性 (.attr())
<a href="http://www.google.com" class="button" data-info="The worlds most popular search engine">Google</a>
$('.button').click(function(e) {
e.preventDefault();
thisdata = $(this).attr('data-info');
console.log(thisdata);
});
来自here的示例和信息