在我的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中),但我还是尝试了一下。
它成功了!
有人能解释一下原因吗?
https://stackoverflow.com/questions/23592030
复制相似问题