如何做“如果点击了其他.." [英] How to do "If Clicked Else .."
本文介绍了如何做“如果点击了其他.."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jQuery做类似的事情
I am trying to use jQuery to do something like
if(jQuery('#id').click) {
//do-some-stuff
} else {
//run function2
}
但是我不确定如何使用jQuery吗?任何帮助将不胜感激.
But I'm unsure how to do this using jQuery ? Any help would be greatly appreciated.
我正在尝试运行一个函数,如果已单击#id,特别是未单击它,则运行函数2吗?即我需要先检查#id在运行function2
吗?
I'm trying to run a function if the #id has been clicked and specifically it is has not been clicked then run the function 2 ? i.e. I need to check firstly that the #id has been clicked before running function2
?
推荐答案
您应该避免使用全局变量,而更喜欢使用.data()
You should avoid using global vars, and prefer using .data()
所以,您会这样做:
jQuery('#id').click(function(){
$(this).data('clicked', true);
});
然后,检查它是否被单击并执行操作:
Then, to check if it was clicked and perform an action:
if(jQuery('#id').data('clicked')) {
//clicked element, do-some-stuff
} else {
//run function2
}
希望这会有所帮助.干杯
Hope this helps. Cheers
这篇关于如何做“如果点击了其他.."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文