【发布时间】:2013-07-17 13:58:44
【问题描述】:
我正在使用这个 div 代码
<div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div>
并尝试打印像
这样的值 japp.init = function () {
console.log($("div").data("role"));
console.log($("div").data("lastValue"));
console.log($("div").data("hidden"));
console.log($("div").data("options").name);
});
如果我将上面的 div 标签直接放在 body 中,这可以正常工作,但是当我将 div 标签放在任何其他 div 标签中时,它不起作用并显示未定义。
<div class="page">
<div data-role="page" data-last-value="43" data-hidden="true" data- options='{"name":"John"}'></div>
</div>
上述 html 的控制台打印未定义。
如果有什么不清楚的地方请告诉我
【问题讨论】:
-
顺便说一句,在你的第二个 html 中有一个空格,在
data- options='{"name":"John"}'。我不知道它是否对代码有任何影响,但你可以检查一下;)
标签: javascript jquery jquery-selectors jquery-data