2013-02-15 102 views
0

我嘗試保持DIV 1隱若DIV 2是展示,而2區是隱藏的,當我想DIV 1所示。我嘗試這樣做在JavaScript,但它不是爲我工作:(隱藏一個div如果另一個顯示其他相反?

有人可以告訴我在哪裏我去錯了感謝

<script> 
$(".profile-banner2").hide(); 
    $(document).ready(function(){ 
     if ($('.infobox-profile').is(":visible")) { 
      $(".profile-banner2").hide(); 

       } else if ($('.infobox-profile').is(":hidden")) { 
      $('.profile-banner2').show(); 

     } 
    }); 
</script> 
+0

不應該第二個條件包括'$其中工程(」簡介-banner1" )。隱藏()'? – 2013-02-15 06:03:56

+0

1)你需要一個像點擊左右的事件和2)不要把任何語句訪問Dom對象以外的document.ready 3)你需要切換 – mplungjan 2013-02-15 06:07:15

回答

0

嘗試這樣的:

<div id="div1"> this is first div </div> 
<div style="display:none"> this is second div</div> 

<input type="submit" value="toggle" id="toggle"/> 

和腳本:

$(function(){ 
    $("#toggle").click(function(){ 
    $("div").toggle(); 
    }); 

}); 

這裏是fiddle


編輯

檢查更新fiddle,如果在HTML中的任意位置單擊

+0

謝謝,但我希望它作爲文檔準備功能而不是點擊 – 2013-02-15 06:41:23

+0

如果你把它放在文檔中,那麼你只會看到'this is second div'' – 2013-02-15 07:37:18