2013-04-22 105 views
0

什麼我試圖存檔是顯示/隱藏一個div如果輸入字段某處集中。顯示作品,但它不會隱藏它。即時通訊新的jQueryjQuery一個元素事件影響另一個

我不能弄清楚我的代碼最新錯誤。

$(document).ready(function(){ 

if ($("#a").has(":focus")){ 
     $("#b").css("visibility","visible");} 
    else { 
     $("#b").css("visibility","hidden");} 
}); 

HTML:

<ul> 
<li id="a" > 
    <div class="label"> 
     <label id="company" class="description" for="company">Company</label> 
    </div> 
    <div class="input"> 
     <input id="inputcompany" name="company" class="element text medium" type="text" maxlength="255" value=""/> 
    </div> 
</li> 
<li id="b" style="visibility: hidden;"> 
    <div class="label"> 
     <label class="description" for="company">Name </label> 
    </div> 
    <div class="input"> 
     <input id="company" name="company" class="element text medium" type="text" maxlength="255" value=""/> 
    </div> 
</li> 

對不起球員,塞康裏應該是ID爲 「B」。

+0

你需要用它來響應用戶的一些事件發生,對不對?就目前而言,當文檔加載時,測試僅應用一次。 – 2013-04-22 09:07:32

回答

1

試試這個,

$('#a input').focus(function() { 
    $("#b").css("visibility","visible"); 
}).blur(function() { 
    $("#b").css("visibility","hidden"); 
}); 
+0

這很好。謝謝! – 2013-04-22 10:23:48

2

JavaScript代碼將只執行一次。看起來好像你想要一個事件處理程序 - 只要#a失去/獲得重點就會執行的代碼。

$('#a').focus(function() { 
    $("#b").css("visibility","visible"); 
}).blur(function() { 
    $("#b").css("visibility","hidden"); 
}); 
+0

+1 [模糊](http://api.jquery.com/blur/)! – soyuka 2013-04-22 09:14:27

1

我認爲你這樣做了錯誤的方式,如果你想顯示下一個李時hovering第一位的,我會做這樣的:

//For example on mouseover the #a 
$('#a').on({ 
    mouseover:function() { 
     $('#b').css("visibility","visible"); //we show the #b 
    }, 
    //on mouseleave 
    mouseleave: function() { 
     if($('#a').find('input').val().length == 0) { //if nothing was entered in the first input 
      $('#b').css("visibility","hidden"); //hide the #b 
     } 
    } 
}); 

看到一個live example here

希望這是幫助。

相關問題