jQuery .data()不起作用,但是.attr()起作用

编程入门 行业动态 更新时间:2024-10-17 06:30:05
本文介绍了jQuery .data()不起作用,但是.attr()起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请原谅我没有对此做更多具体说明.我有一个奇怪的错误.在文档加载后,我循环了一些最初具有data-itemname=""的元素,并使用.attr("data-itemname", "someValue")设置了这些值.

Forgive me for not being more specific on this. I have such a strange bug. After the doc loads, I loop some elements that originally have data-itemname="", and I set those values using .attr("data-itemname", "someValue").

问题:当我以后循环通过那些元素时,如果我执行elem.data().itemname,则得到"",但是如果我执行elem.attr("data-itemname"),则得到"someValue".就像jQuery的.data() getter只获取最初设置为包含某些值的元素,但是如果它们最初为空,然后又被设置,则.data()以后不会获取该值.

Issue: When I later loop thru those elements, if I do elem.data().itemname, I get "", but if I do elem.attr("data-itemname"), I get "someValue". It's like jQuery's .data() getter only gets elements that are set initially to contain some value, but if they are originally empty, and later set, then .data() doesn't get the value later on.

我一直在尝试重新创建此错误,但未能成功.

I've been trying to recreate this bug but have not been able to.

修改

我已经重新创建了错误! jsbin/ihuhep/edit#javascript,html,live

I have recreated the bug! jsbin/ihuhep/edit#javascript,html,live

此外,链接上方的摘录...

Also, snippets from above link...

JS:

var theaters = [ { name: "theater A", theaterId: 5 }, { name: "theater B", theaterId: 17 } ]; $("#theaters").html( $("#theaterTmpl").render(theaters) ); // DOES NOT WORK - .data("name", "val") does NOT set the val var theaterA = $("[data-theaterid='5']"); theaterA.find(".someLink").data("tofilllater", "theater5link"); // this does NOT set data-tofilllater $(".someLink[data-tofilllater='theater5link']").html("changed link text"); // never gets changed // WORKS - .attr("data-name", "val") DOES set val var theaterB = $("[data-theaterid='17']"); theaterB.find(".someLink").attr("data-tofilllater", "theater17link"); // this does set data-tofilllater $(".someLink[data-tofilllater='theater17link']").html("changed link text");

HTML:

<body> <div id="theaters"></div> </body> <script id="theaterTmpl" type="text/x-jquery-tmpl"> <div class="theater" data-theaterid="{{=theaterId}}"> <h2>{{=name}}</h2> <a href="#" class="someLink" data-tofilllater="">need to change this text</a> </div> </script>

推荐答案

几天前,当使用.data()和.attr('data-name')获取HTML5数据属性时,我遇到了类似的错误".

I ran into a similar "bug" a few days ago when working with .data() and .attr('data-name') for HTML5 data attributes.

您描述的行为不是错误,而是设计使然.

The behavior you're describing is not a bug, but is by design.

.data()调用非常特殊-它不仅检索HTML5数据属性,而且还尝试评估/解析该属性.因此,使用类似data-myjson='{"hello":"world"}'的属性时,通过.data()检索时将返回Object,而通过.attr()检索时将返回字符串. 请参阅jsfiddle示例.

The .data() call is special - not only does it retrieve HTML5 data attributes it also attempts to evaluate/parse the attributes. So with an attribute like data-myjson='{"hello":"world"}' when retrieved via .data() will return an Object while retrieval via .attr() will return a string. See jsfiddle example.

由于.data()进行了额外的处理,因此jQuery将属性评估的结果存储在$.cache中-毕竟,一旦对数据属性进行了评估,那么在每个.data()调用中重新评估就很浪费了-特别是因为数据变量可以包含复杂的JSON字符串.

Since .data() does extra processing jQuery stores the results of attribute evaluation in $.cache - after all, once a data attribute has been evaluated it would be wasteful to re-evaluate on every .data() call - especially since data variables can contain complex JSON strings.

我说的所有都是这样:通过.data()检索属性后,.attr('data-myvar', '')所做的任何更改都不会在随后的.data()调用中看到. jsfiddle/PKwdr/"rel =" noreferrer>在jsfiddle上进行测试.

I said all that to say the following: After retrieving an attribute via .data() any changes made by .attr('data-myvar', '') will not be seen by subsequent .data() calls. Test this out on jsfiddle.

为避免出现此问题,请勿混用.data和.attr()呼叫.使用一个或另一个.

To avoid this problem don't intermix .data and .attr() calls. Use one or the other.

更多推荐

jQuery .data()不起作用,但是.attr()起作用

本文发布于:2023-11-26 20:42:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   jQuery   data   attr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!