我试图在DOM中添加一个HTML 5数据属性.我在jQuery网站上看到的数据属性格式是:
$('.pane-field-related-pages ul').data("role") === "listview";
但这似乎没有添加任何东西到DOM?
解决方法
阅读
this article
从文章
jQuery.com – “The .data() method@H_404_16@ allows us to attach data of any type@H_404_16@ to DOM elements in a way that is safe@H_404_16@ from circular references and therefore@H_404_16@ from memory leaks.”
With the introduction of HTML5,we can@H_404_16@ use it as attribute as well. For@H_404_16@ example
<div data-role="page" data-hidden="true" data-options='{"name":"John"}'></div> //We can call it via: $("div").data("role") = "page"; $("div").data("hidden") = true; $("div").data("options").name = "John";
替代方式
$("div").data("role","page"); $("div").data("hidden","true"); $("div").data("role",{name: "John"});