Javascript JQuery:除了某个元素之外,在任何地方都单击?
我有一个文本框和一个div。当用户在文本框或div外单击时,我想隐藏div 除了document.click之外还有其他方法吗?单击以处理用户在外部单击的内容。因为在某些控件中,提供了event.stoppropagation,它在单击时不会触发文档单击事件 谢谢创建一个覆盖div(请参阅),然后向覆盖div添加一个单击事件,该事件将覆盖div隐藏在文本框下方并销毁覆盖div。创建一个覆盖div(请参阅),然后向覆盖div添加一个单击事件,该事件将覆盖div隐藏在文本框下方并销毁覆盖div。Javascript JQuery:除了某个元素之外,在任何地方都单击?,javascript,jquery,Javascript,Jquery,我有一个文本框和一个div。当用户在文本框或div外单击时,我想隐藏div 除了document.click之外还有其他方法吗?单击以处理用户在外部单击的内容。因为在某些控件中,提供了event.stoppropagation,它在单击时不会触发文档单击事件 谢谢创建一个覆盖div(请参阅),然后向覆盖div添加一个单击事件,该事件将覆盖div隐藏在文本框下方并销毁覆盖div。创建一个覆盖div(请参阅),然后向覆盖div添加一个单击事件,该事件将覆盖div隐藏在文本框下方并销毁覆盖div。 &
<script type="text/javascript">
hideDiv()
{
document.getElementById("divId").style.display = "none";
}
</script>
<input type="text" onblur='javascript:hideDiv()'>
希迪夫()
{
document.getElementById(“divId”).style.display=“无”;
}
我想这应该行。
希迪夫()
{
document.getElementById(“divId”).style.display=“无”;
}
我认为这应该行得通。因为您提到在单击事件的页面不同部分有
event.stopPropagation()
,所以文档。单击将无法隐藏文本框
为什么不使用document.mousedown
?它会很好用的
$(document).mousedown(function(){
$('textboxSelector').hide();
});
确保停止mousedown
事件从textbox
及其包含的div
传播,因为您提到在单击事件文档的页面的不同部分有event.stopPropagation()
。单击将无法隐藏文本框
// This means if you click anywhere on the document once, it'll hide the div
$(document).one("click", function() {/*Do stuff, hide div*/});
// This overrides the previous line for just the textarea and div, therefore making this block of code only apply to everything but the textarea and div
$('textbox, div').click(function(){return false;});
为什么不使用document.mousedown
?它会很好用的
$(document).mousedown(function(){
$('textboxSelector').hide();
});
确保您停止mousedown
事件从textbox
及其包含的div传播。我不认为有onnfocus
,有onblur
。我不认为有onnfocus
,有onblur
。
// This means if you click anywhere on the document once, it'll hide the div
$(document).one("click", function() {/*Do stuff, hide div*/});
// This overrides the previous line for just the textarea and div, therefore making this block of code only apply to everything but the textarea and div
$('textbox, div').click(function(){return false;});