我有以下 HTML:
<span loopx='{"operator":"maxis"}'>hello</span>
如果我想检索它的属性值{"operator":"maxis"}
,我可以简单地使用
$('span[loopx]').each(function(index) {
var attr = $(this).attr('loopx');
// attr is the string "{"operator":"maxis"}"
});
但是,对于下面的 HTML
<span loop='{"operator":"maxis"}'>world</span>
我得到以下结果:
$('span[loop]').each(function(index) {
var attr = $(this).attr('loop');
// attr is the string "loop"
});
我在想,loop
是不是span中的一个特殊关键字?
最佳答案
span
中没有 loop
属性元素:仅限全局属性。
对于自定义属性,您应该使用 data
attributes如 HTML5 中所定义。
如果您将属性命名为 data-loop
,您可以通过 element.dataset.loop
在 native 访问它。
关于javascript - "loop"是跨度内的特殊关键字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30753018/