2015-04-17 50 views
0

我有這個腳本顯示/隱藏div。任何人都可以請解釋一下如何讓它只顯示一個div?顯示一個div並隱藏其餘使用javascript

<script> 
function Show_Div(Div_id) { 
if (false == $(Div_id).is(':visible')) { 
    $(Div_id).show(); 
} 
else { 
    $(Div_id).hide(); 
} 
} 
</script> 

和鏈接...

onClick="Show_Div(Div_1) 

謝謝!

+0

看到這個:http://stackoverflow.com/questions/8655741/javascript-hide-all-other-divs –

+0

如果你已經使用jQuery,爲什麼不'$ ( 「DIV」)隱藏(); $( 「#myDiv」)。顯示()'? – Huey

+0

感謝'客人'在您的鏈接中使用了信息並解決了我的問題:)感謝其他人爲您提供幫助! – dave

回答

1

嘗試使用此

$(document).ready(function(){ 
    $('.parent div').hide(); // hide div's on load using parent class as a starting point  
    $('#nav a').click(function() { // on the anchor clicks that are inside div with id=nav 
     var $div = $('.parent div').eq($(this).index('#nav a')); // get the relevant div 
     $div.show(); // show the relevant div 
     $('.parent div').not($div).hide(); // hide all but the relevant div 
    });​ 
}): 
+0

感謝海軍,我最終使用了上面的鏈接,但感謝您的幫助! – dave

-1

如果我正確理解你的問題,也許你可以試試這個方法:

<script> 
function Show_Div(Div_id) { 
    if (false == $(Div_id).is(':visible')) { 
     $(this).show(); 
    } 
    else { 
     $(this).hide(); 
    } 
} 
</script> 

如果您使用this

$(this).show(); 

只有一個將切換您的點擊〜 但以這種方式:

$(Div_id).show(); 

你將得到一個目標數組,因爲Jquery的選擇器會選擇一個目標數組。

希望它會幫助你〜

+0

感謝錢家豪,最後我用了上面的鏈接,但也感謝你的幫忙! – dave