用于$(this).show();使当前元素可见并隐藏其余元素。
$(document).ready(function(){
$('.z').blur(function(){
$('.z').hide();
$(this).show();
});
});
您可以从选择器中排除当前元素以使用.notlike 隐藏元素$('.z').not(this).hide();
$(document).ready(function(){
$('.z').blur(function(){
$('.z').not(this).hide();
});
});
根据 OP 的变化进行编辑
如果元素失去焦点,您可以添加属性来存储并使用它来确定所有元素是否都失去焦点。
现场演示
$(document).ready(function(){
$('.z').blur(function(){
$(this).attr('lostFocus', 'true');
if($('.z').length == $('.z[lostFocus="true"]').length)
$('.z').hide();
});
});
请注意为您的 html 元素的属性添加引号,如下所示。
<input type="text" class="z">
<input type="text" class="z">
<input type="text" class="z">
<input type="text" class="z">