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