[英]Jquery .on event.trigger not refreshed on second click
Hi I'm getting trouble with one strange behavior with this: 嗨,我遇到了一种奇怪的行为:
I got a link which feeds some data attributes to another link(back link of a slider) 我有一个链接,该链接将一些数据属性提供给另一个链接(滑块的后向链接)
<div class="col-sm-4 etapes etape1 btn-back"
data-classtarget="row_containerHover"
data-currentcontainerid="row_containerHover2"></div>
and the back link code 和反向链接代码
$('body').on('click touchstart', '.btn-back', function (e) {
//$('.btn-back').on('click touchstart', function (e) {
//console.log('click btn-back');//test ok
//var container = $(e.target).closest('.slide_containerHover');
var container = $("#"+$(e.currentTarget).data('currentcontainerid'));
console.log("current container : "+container.attr("id"));
var container_class_target = $(e.target).data("classtarget");
var prev_container = $("#"+container_class_target);
console.log("prev container : "+prev_container.attr("id"));
//On récup le niveau du container ds l'arborescence
/*var level_container = container.data('level');
var prev_level = parseInt(level_container - 1);*/
//On récup le container précédent
var decalX = parseInt(parseInt($(window).outerWidth() / 2)
+ parseInt(container.outerWidth() / 2));
console.log("ledecalX : "+decalX);//test
container.animate({
left: "+=" + decalX
}, 500, 'easeInOutQuad');
//console.log('row_containerHover outerWidth : '+decalX);//test
setTimeout(function () {
prev_container.animate({
left: "0px"
}, 500, 'easeInOutQuad'), 100
});
}
First time it's ok, my data attributes are correct. 第一次没问题,我的数据属性正确。 When I click another link, the data attributes are changed in my html, but the console test doesn't display the changed values of
$(e.currentTarget).data('currentcontainerid')
当我单击另一个链接时,数据属性在html中已更改,但控制台测试未显示
$(e.currentTarget).data('currentcontainerid')
的更改值。
Is there a way to get the refreshed data attributes of $(e.currentTarget)
? 有没有办法获取
$(e.currentTarget)
的刷新数据属性?
Thanks For all 谢谢大家
Finally I've found a way by removing it with the jquery .removeData at the end of procedure : 最后,我找到了一种方法,可以在过程结束时使用jquery .removeData删除它:
$(e.currentTarget).removeData('classTarget');
$(e.currentTarget).removeData('currentcontainerid');
Now it works like a charm ^_^ thanks for your Help 现在它就像一个魅力^ _ ^感谢您的帮助
You are looking for dataset
instead of data()
. 您正在寻找
dataset
而不是data()
。 With dataset
you can set and get values dinamically. 使用
dataset
您可以动态设置和获取值。
$(selector).get(0).dataset.currentcontainerid; // returns data-currentcontainerid value
$(selector).get(0).dataset.currentcontainerid = "new value"; // set the new value
I use get(0)
to return the DOM object instead the jQuery object. 我使用
get(0)
返回DOM对象而不是jQuery对象。 You can make it with pure javascript: 您可以使用纯JavaScript实现:
document.getElementById("element").dataset.currentcontainerid = "new value"; // set the new value
More information: 更多信息:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/dataset
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.