3

In the code below if you click on <input type="text" id="one" /> and a "red block" appears. If you focus out then the "red block" disappears.

How do I make it so that focusout wont fire if "red block" or <input type="text" id="two" /> are the next focused elements?

Demo

JavaScript

$('#one').focus(function () {
    $('#divRemove').show();
});

$('#one').focusout(function () {
    $('#divRemove').hide();
});

$('#divRemove').click(function(){
    alert($(this).text());    
});

HTML

<input type="text" id="one" />_______
<input type="text" id="two" />
<br/>
<br/>
<div id="divRemove" style="width:100px;height:100px;background:red; display:none;">remove on focus out</div>
4

1 回答 1

3

您可以同时关注#one 和#two

$('#one,#two').focus(function () {
    $('#divRemove').show();
}).focusout(function () {
    $('#divRemove').hide();
});

演示

更新

除非你给它一个像这样的自定义索引,否则你不能真正关注一个 divtabindex="0"

<div id="divRemove" tabindex="0"

然后在 jQuery 中执行此操作

$('#one,#two,#divRemove').focus(function () {
    $('#divRemove').show();
}).focusout(function () {
    $('#divRemove').hide();
});

演示

于 2013-10-08T08:13:14.327 回答