首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jQuery:获取数据属性

jQuery:获取数据属性
EN

Stack Overflow用户
提问于 2014-05-11 19:30:49
回答 3查看 181.8K关注 0票数 71

在我的html中,我有一个span元素:

<span class="field" data-fullText="This is a span element">This is a</span>

我想要获取data-fullText属性。我尝试了这两种方法,但都不起作用(这两种方法都返回undefined):

$('.field').hover(function () {
    console.log('using prop(): ' + $(this).prop('data-fullText'));
    console.log('using data(): ' + $(this).data('fullText'));
});

然后我搜索并找到了这些问题:How to get the data-id attribute?jquery can't get data attribute value

两者的答案都是"Use .attr('data-sth') or .data('sth')"

我知道.attr()已被弃用(在我使用的jquery-1.11.0中),但我还是尝试了一下。

它成功了!

有人能解释一下原因吗?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23592030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档