jQuery 如何做“如果点击其他...”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6629297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do "If Clicked Else .."
提问by Tom
I am trying to use jQuery to do something like
我正在尝试使用 jQuery 来做类似的事情
if(jQuery('#id').click) {
//do-some-stuff
} else {
//run function2
}
But I'm unsure how to do this using jQuery ? Any help would be greatly appreciated.
但我不确定如何使用 jQuery 做到这一点?任何帮助将不胜感激。
Edit: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
?
编辑:如果 #id 已被点击,特别是它没有被点击,我正在尝试运行一个函数,然后运行函数 2 ?即我需要先检查#id 是否在运行前被点击了function2
?
回答by Edgar Villegas Alvarado
You should avoid using global vars, and prefer using .data()
您应该避免使用全局变量,而更喜欢使用 .data()
So, you'd do:
所以,你会这样做:
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
希望这可以帮助。干杯
回答by James Long
The way to do it would be with a boolean at a higher scope:
这样做的方法是在更高的范围内使用布尔值:
var hasBeenClicked = false;
jQuery('#id').click(function () {
hasBeenClicked = true;
});
if (hasBeenClicked) {
// The link has been clicked.
} else {
// The link has not been clicked.
}
回答by Rajeev
var flag = 0;
$('#target').click(function() {
flag = 1;
});
if (flag == 1)
{
alert("Clicked");
}
else
{
alert("Not clicked");
}
回答by therin
A click is an event; you can't query an element and ask it whether it's being clicked on or not. How about this:
点击是一个事件;你不能查询一个元素并询问它是否被点击。这个怎么样:
jQuery('#id').click(function () {
// do some stuff
});
Then if you really wanted to, you could just have a loop that executes every few seconds with your // run function..
然后,如果你真的想要,你可以有一个循环,每隔几秒执行一次 // run function..
回答by MacMac
You may actually mean hovering the element by not clicking, right?
您实际上可能是指通过不单击来悬停元素,对吗?
jQuery('#id').click(function()
{
// execute on click
}).hover(function()
{
// execute on hover
});
Clarify your question then we'll be able to understand better.
澄清你的问题,然后我们才能更好地理解。
Simply if an element isn't being clicked on, do a setInterval
to continue the process until clicked.
简单地,如果一个元素没有被点击,请执行 asetInterval
继续该过程,直到被点击。
var checkClick = setInterval(function()
{
// do something when the element hasn't been clicked yet
}, 2000); // every 2 seconds
jQuery('#id').click(function()
{
clearInterval(checkClick); // this is optional, but it will
// clear the interval once the element
// has been clicked on
// do something else
})
回答by citizen conn
This is all you need: http://api.jquery.com/click/
这就是你所需要的:http: //api.jquery.com/click/
Having an "else" doesn't apply in this scenario, else would mean "did not click", in which case you just wouldn't do anything.
在这种情况下,“其他”不适用,否则意味着“没有点击”,在这种情况下,您将什么也不做。
回答by Dr.Molle
Maybe you're looking for something like this:
也许你正在寻找这样的东西:
$(document).click(function(e)
{
if($(e.srcElement).attr('id')=='id')
{
alert('click on #id');
}
else
{
alert('click on something else');
}
});
You may retrieve a pointer to the clicked element using event.srcElement
.
您可以使用 检索指向被点击元素的指针event.srcElement
。
So all you have to do is to check the id-attribute of the clicked element.
所以你所要做的就是检查被点击元素的 id 属性。
回答by Freezingden
if you press x, x amount of times you will get the division of the checkbox by y which results into a jquery boxcheck which would be an invalid way to do this kind of thing. I hope you will have a look at this before using the .on() functions
如果您按 x, x 次,您将获得复选框除以 y 的结果,这将导致 jquery boxcheck,这将是执行此类操作的无效方法。我希望你在使用 .on() 函数之前先看看这个