我有以下代码 http://jsfiddle.net/FrVy4/1/:
HTML:
<input type='text' class='a' />
<div class='inst' tag='a'></div>
<input type='text' class='b' />
<div class='inst' tag='b'></div>
<input type='text' class='c' />
<div class='inst' tag='c'></div>
JS:
$(function() {
$('.inst').click(function() {
alert($(this).attr('tag') + ' clicked');
});
$('[type=text]').focus(function() {
show_inst($(this).attr('class'));
}).blur(function() {
//hide_inst($(this).attr('class'));
});
function show_inst(tag) {
$('div.inst[tag=' + tag + ']').html(tag + ' instructions');
}
function hide_inst(tag) {
$('div.inst[tag=' + tag + ']').html('');
}
});
CSS:
.inst {
width: 200px;
height: 100px;
border: 1px solid black;
margin: 10px;
cursor: pointer;
}
它工作正常:当inst
单击后,我会看到警报消息,并且当输入成为焦点时,会出现说明。
现在我希望不相关的指令在模糊时消失。所以我尝试在里面添加注释行blur()
。它不会那样工作,因为blur()
首先调用并删除指令,所以如果我单击指令 - 什么也不会发生。
我该如何解决这个问题?
如果您唯一的问题是指令在单击之前被隐藏,请考虑添加一点延迟。这基本上会导致您的隐藏指令在处理点击后执行。
像这样的东西:
$('[type=text]').focus(function() {
show_inst($(this).attr('class'));
}).blur(function() {
setTimeout(function(){
hide_inst($(this).attr('class'));
}, 50); // make this happen after any other events
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)