微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jQuery – 如何将HTML 5数据属性添加到DOM中

我试图在DOM中添加一个HTML 5数据属性.我在jQuery网站上看到的数据属性格式是:
$('.pane-field-related-pages ul').data("role") === "listview";

但这似乎没有添加任何东西到DOM?

如何将上述数据角色添加到相关的ul标签中?

解决方法

阅读 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"});

原文地址:https://www.jb51.cc/jquery/180412.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐