题
如果文本框失去焦点,我只想隐藏一个div,除非用户单击另一个特定div。如果用户单击该特定的div,则焦点并不会触发隐藏的div.box。
以下是我的伪代码评论的代码。有任何想法吗?
textInput.focusout(function() {
// So long you didn't click div.stop, then
$('div.box').hide();
});
解决方案
var flag = false;
$('div.stop').click(function() {flag = true;});
textInput.focusout(function() {
// So long you didn't click div.stop, then
if(!flag)
$('div.box').hide();
});
如果您想避免添加标志变量,则可以使用jQuery的 。数据 为了存储标志值,例如 div.stop
元素,例如:
$('div.stop').click(function() {$(this).data('clicked', true);});
// ...
if(!$('div.stop').data('clicked'))
// ...
编辑
如果您想允许文本框有焦点的情况,然后单击 div.stop
, ,您不希望它隐藏,然后您可以尝试以下操作:
$('div.stop').click(function() {$('div.box').stop();});
textInput.focusout(function() {
// So long you didn't click div.stop, then
$('div.box').delay(200).hide();
});
其他提示
$(document).bind('click',function(e) {
if ($(e.target).closest('div.box').length) return;
$('div.box').hide();
});
试试这个!
var flag = false;
$('#inputID').blur(function(){
$('div.box').toggle(flag);
});
$('#someDIVid').click(function(){
flag = true;
});
简单的演示
添加注释:
.toggle(showorhide)
Soundorhide 布尔值指示是显示还是隐藏元素。如果此参数为
true
, ,然后匹配 显示元素;如果false
, , 这 元素是隐藏的.
不隶属于 StackOverflow