[英]jQuery if else statements and data()
I've been struggling over this for almost 5 hours and finally I've got a clue. 我已经苦苦挣扎了近5个小时,最后我有了一个线索。
Here's a code that I'm using on my website: 这是我在我的网站上使用的代码:
jQuery(document).ready(function(){
jQuery(".thumbnail").bind("click",function(){
var elem = jQuery(this);
if((elem).data('test')) {
alert('data is test');
elem.data('test',false);
}
else {
alert('data is not test');
elem.data('test',true);
}
});
});
Here's jfiddle. 这是jfiddle。
http://jsfiddle.net/Fe678/ http://jsfiddle.net/Fe678/
When you click on "thumbnail" it should fire one alert (either "data is test" or "data is not test"). 当您点击“缩略图”时,它应该触发一个警报(“数据是测试”或“数据不是测试”)。
When I try the same exact code on my WordPress page (localhost) it shows TWO alerts one after another - "data is not set" and then "data is set" - what may cause this strange behavior? 当我在我的WordPress页面(localhost)上尝试相同的确切代码时,它会一个接一个地显示两个警报 - “数据未设置”然后“数据已设置” - 可能导致这种奇怪行为的原因是什么?
Unfortunately I can't provide the source. 不幸的是我无法提供来源。 I'm hoping this is something common and maybe some jQuery wizard will know the answer? 我希望这是常见的,也许一些jQuery向导会知道答案吗? Somehow...? 不知何故...?
I don't think it's a jQuery bug, but one of the following: 我不认为这是一个jQuery错误,但以下之一:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.