admin管理员组文章数量:1191725
i have a div with data attribut like
<div class='p1' data-location='1'></div>
and i have script like
$('button').click(function(){
var loc = $('.p1').data('location');
alert('data location is'+loc);//SHOW THE DATA
var num = 10;
var count = loc;
var element = $('.p1');
var intv = setInterval(anim,1000);
function anim(){
count++;
num--;
if(count==37){count = 1;}
if(num==1){clearInterval(intv);}
$(element).animateCSS('bounceOut',{
callback: function(){
$(element).attr('data-location',count);
$(element).animateCSS('bounceIn');
}
});
}
anim();
});
with the script above the data-location attribute will be updated to 10, but if i click the button again, the data-location is still 1
i have a div with data attribut like
<div class='p1' data-location='1'></div>
and i have script like
$('button').click(function(){
var loc = $('.p1').data('location');
alert('data location is'+loc);//SHOW THE DATA
var num = 10;
var count = loc;
var element = $('.p1');
var intv = setInterval(anim,1000);
function anim(){
count++;
num--;
if(count==37){count = 1;}
if(num==1){clearInterval(intv);}
$(element).animateCSS('bounceOut',{
callback: function(){
$(element).attr('data-location',count);
$(element).animateCSS('bounceIn');
}
});
}
anim();
});
with the script above the data-location attribute will be updated to 10, but if i click the button again, the data-location is still 1
Share Improve this question edited Feb 5, 2015 at 3:45 Barmar 781k56 gold badges545 silver badges659 bronze badges asked Feb 5, 2015 at 3:43 MamenMamen 1,4367 gold badges22 silver badges46 bronze badges4 Answers
Reset to default 19The first time that you use .data()
to access a data-*
attribute, the value of that attribute is cached internally by jQuery, and .data()
uses the cache from then on. Updating the attribute with .attr()
does not update the cache, you need to use .data()
to update it. That's why you need to use
$(element).data('location', count);
to update it.
$(element).attr('data-location',count);
is different than
$(element).data('location',count);
so, use the second instead.
Check Data vs Attr for details.
you are setting the attr property and not data using .attr('data-location',count)
. to get the attribute you need to use .attr('data-location')
:
var loc = $('.p1').attr('data-location');
I know this is an old question, but I'm visiting in 2022 and really this shouldn't be done in jQuery. The caching is a bad idea, and shouldn't need to update it using their magic method.
The best method for accessing data attributes is via the JavaScript element.dataset
property.
By simply calling element.dataset
rather than $(element).data()
, you will always get the latest data object.
本文标签: javascriptget wrong value in data attribute jqueryStack Overflow
版权声明:本文标题:javascript - get wrong value in data attribute jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738450155a2087440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论