单击jQuery中的链接时如何防止blur()运行? [英] how to prevent blur() running when clicking a link in jQuery?
问题描述
我有:
<input type="text" />
和
$('input').blur(function(){
alert('stay focused!');
});
我想通过单击锚点元素来防止模糊"时模糊功能的运行.
I want to prevent the blur function running when I'm "blurring" by clicking on an anchor element.
I.E.如果我切换到其他输入,请单击页面上的某处,等等,我希望模糊触发,但是如果我单击链接,则不希望其触发.
I.E. if i tab to another input, click somewhere on the page etc i want the blur to fire, but if i click a link, I don't want it to fire.
这是容易实现的吗,还是我需要与代表和信号灯谈谈?
Is this easily achievable, or do i need to hack about with delegates and semaphores?
谢谢
推荐答案
我今天也必须自己解决此问题.我发现mousedown事件在blur事件之前触发,因此您要做的就是设置一个变量,该变量指示首先发生mousedown事件,然后适当地管理您的blur事件.>
I had to solve this problem myself today, too. I found that the mousedown event fires before the blur event, so all you need to do is set a variable that indicates that a mousedown event occurred first, and then manage your blur event appropriately if so.
var mousedownHappened = false;
$('input').blur(function() {
if(mousedownHappened) // cancel the blur event
{
alert('stay focused!');
$('input').focus();
mousedownHappened = false;
}
else // blur event is okay
{
// Do stuff...
}
});
$('a').mousedown(function() {
mousedownHappened = true;
});
希望这对您有帮助!
这篇关于单击jQuery中的链接时如何防止blur()运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!