jQuery data()返回undefined,attr()返回整数 [英] jQuery data() returns undefined, attr() returns integer
本文介绍了jQuery data()返回undefined,attr()返回整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
alert($embellishment.data("embellishmentId"));
alert($embellishment.attr("data-embellishmentId"));
第一个警报返回 undefined
,而第二个警报返回一个整数, 3
。
The first alert returns undefined
, whereas the second alert returns an integer, 3
.
我正在使用jQuery 1.7.2版( 数据
已添加1.4版本我相信)
I'm using jQuery version 1.7.2 (data
was added with version 1.4 I believe)
为什么会这样?如果它没有返回正确的值,我应该使用 data()
吗?
Why is this? Should I be using data()
at all if its not returning the right values?
推荐答案
行。我通过解释 jQuery docs 找到了问题。
OK. I found the problem by interpreting jQuery docs.
当你写:
$embellishment.data("embellishmentId");
它由jQuery作为复合属性处理:
it is handled by jQuery as compound attribute:
<div data-embellishment-id="3"></div>
因此,要解决此问题,您可以在数据键中使用小写,否则它只会解决不同的问题属性。
So, to solve the problem you can use lower case in the data key otherwise it just addresses the different attribute.
<!-- HTML -->
<div data-embellishmentid="3"></div>
// JavaScript
$embellishment.data("embellishmentid");
这篇关于jQuery data()返回undefined,attr()返回整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文