简体   繁体   English

jQuery .blur()-不要隐藏活动div

[英]jQuery .blur() - Don't hide active div

Why does this code hide the div that's used as the jQuery selector on blur() if I click inside it? 如果我在内部单击,此代码为什么会隐藏用作blur()上的jQuery选择器的div? Blur() should only hide it if I click outside it. 如果在外部单击,则Blur()应该仅将其隐藏。

HTML trigger: HTML触发器:

<div class="header">To-Do <a class="triggernewtodo">Add a task...</a></div>

li to show/hide: li显示/隐藏:

<li class="addnewtodo">
<form>
    <textarea class="addtodotextarea"></textarea>
    <div class="controlarea">
    <input class="primary" name="commit" type="submit" value="Add" />
    </div>
</form>
</li>

jQuery: jQuery的:

// Show/Hide New Todo form
$('a.triggernewtodo').click(function() {
    $('li.addnewtodo').show()
    $('textarea.addtodotextarea').focus();
});

$('li.addnewtodo').live("blur", function() {
    $(this).hide();
})

My guess is that it has something to do with the .focus() placed on the textarea, because if I take out that .focus(), it works properly until I click inside the textarea then click out (still inside the list item) and it hides the whole list item. 我的猜测是,它与放置在textarea上的.focus()有关,因为如果我取出该.focus(),它会正常工作,直到我在textarea内单击然后单击(仍然在列表项内)它隐藏了整个列表项。 Any ideas? 有任何想法吗? What am I doing wrong? 我究竟做错了什么?

I suspect you're seeing an event that's "bubbled up" from within that LI. 我怀疑您在该LI内看到了一个“冒泡”的事件。 Essentially, a "blur" event from the INPUT, say, triggers all the "blur" handlers on that input. 本质上说,来自INPUT的“模糊”事件会触发该输入上的所有“模糊”处理程序。 Then it triggers all the "blur" handlers on the FORM. 然后,它触发FORM上的所有“模糊”处理程序。 Then it triggers the "blur" handlers on the LI - hiding it. 然后,它会触发LI上的“模糊”处理程序-将其隐藏。

The event continues all the way up the DOM tree unless you call stopPropagation to prevent this. 除非您调用stopPropagation来阻止该事件 ,否则该事件将一直持续到DOM树。 In your case, you should be able to use the event.target property to see if the event actually originated at the LI, and only hide it in that situation (warning: untested): 在您的情况下,您应该能够使用event.target属性来查看事件是否真正起源于LI,并仅在那种情况下将其隐藏(警告:未经测试):

$('li.addnewtodo').live("blur", function(event) {
    if(event.target == this)
    {
        $(this).hide();
    }
});

Hope that helps! 希望有帮助!

PS: See the page on the blur event for additional caveats. PS:有关其他警告,请参见有关模糊事件的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM