2013-07-11 66 views
-1

當我按下我需要顯示第一個div「D1」別人隱藏任何鍵,我按任意鍵再次顯示它謝勝利格「D2」別人隱藏和重新按任意鍵就顯示第三格「d3」其他人隱藏..第六格...不再重複該過程。按鍵顯示在一個股利和其他應隱藏

<div class="objects" id="d1"> 
    <img src="images/d1.jpg" /> 
</div> 

<div class="objects" id="d2"> 
    <img src="images/d2.jpg" /> 
</div> 

<div class="objects" id="d3"> 
    <img src="images/d3.jpg" /> 
</div> 

....... 
....... 

<div class="objects" id="d6"> 
    <img src="images/d6.jpg" /> 
</div> 

當我按下任意鍵我需要顯示1格,然後我按任意鍵顯示第二div..till 6格..

如何做到這一點在JavaScript?

+0

你有什麼迄今..? –

+0

我剛剛嘗試了循環,但它不工作。 – prasanth

+0

安置自己的代碼作爲小提琴 - ** [的jsfiddle(http://jsfiddle.net/)**'這不是working'並沒有真正幫助我們... –

回答

0
<html> 
<head> 
<style type="text/css"> 
    .Div 
    { 
     display: none; 
    } 
</style> 
<script type="text/javascript"> 



    var i = 0; 
    function KeyHandler() { 
     i++; 
     var divs = document.getElementsByClassName("Div"); 

     for (var div = 0; div < divs.length - 1; div++) { 

      divs[div].style.display = 'none'; 
     } 
     var ele = document.getElementById("d" + i); 
     ele.style.display = "block"; 

    } 

</script> 
</head> 
<body onkeyup="KeyHandler()"> 
Press any key.. 
<div id="d1" class="Div"> 
    <h5> 
     Div1</h5> 
</div> 
<div id="d2" class="Div"> 
    <h5> 
     Div2</h5> 
</div> 
<div id="d3" class="Div"> 
    <h5> 
     Div3</h5> 
</div> 
<div id="d4" class="Div"> 
    <h5> 
     Div4</h5> 
</div> 
<div id="d5" class="Div"> 
    <h5> 
     Div5</h5> 
</div> 
<div id="d6" class="Div"> 
    <h5> 
     Div6</h5> 
</div> 
</body> 
</html> 
0

嘗試了這一點

var currentVisible = 0; 
document.onkeyup = function() { 
    // First hide all by class 'objects' 
    // Requires IE9+ FF3+, others are supported 
    var objs = document.getElementsByClassName('objects'); 
    for (var i=0; i<objs.length; i++) { 
     objs[i].style.display = 'none'; 
    } 
    // Show the one we are supposed to show 
    if (++currentVisible == 7) 
     currentVisible = 1; 
    var el = document.getElementById('d' + currentVisible); 
    el.style.display = 'block'; 
} 

100%未經檢驗壽,因爲我有任何小提琴大氣壓的訪問權限。

相關問題