2016-08-05 4 views
-1

이 배열을 반복하고 배열에서 HTML 따옴표로 하나씩 반환 할 수 있습니까? 버튼을 누를 때마다?루프를 반복하고 배열에서 문자열을 반환하십시오.

이 내가 알아낼 수있는 코드의 내 조각 :

var quotes = ['"Time you enjoy wasting is not wasted time."', 
'"You can have it all. Just not all at once."', 
'"They say I am old-fashioned, and live in the past, but sometimes I think 
progress progresses too fast!"']; 

document.getElementById("btn1").addEventListener("click", newQuote); 
function newQuote(){ 
    for (var i = 0; i < quotes.length; i++) { 
    document.getElementById("quote").innerHTML = quotes[i]; 
    } 
} 

답변

0
var quotes = ['"Time you enjoy wasting is not wasted time."', 
'"You can have it all. Just not all at once."', 
'"They say I am old-fashioned, and live in the past, but sometimes I think 
progress progresses too fast!"']; 

var quoteNumber = 0; 
document.getElementById("btn1").addEventListener("click", newQuote); 
function newQuote(){ 
    document.getElementById("quote").innerHTML = quotes[quoteNumber]; 
    quoteNumber++; 
    if(quoteNumber == quotes.length) quoteNumber = 0; 
} 
+0

보다 k 너는 대답 해! – NJV

1

다음 인용 지수을 추적하는 카운터를 사용하여 당신이 도달 한 번 주위를 감싸는 마지막 :

var nextQuote = 0; 
 

 
var quotes = [ 
 
    '"Time you enjoy wasting is not wasted time."', 
 
    '"You can have it all. Just not all at once."', 
 
    '"They say I am old-fashioned, and live in the past, but sometimes I think progress progresses too fast!"' 
 
]; 
 

 
function newQuote() { 
 
    document.getElementById("quote").innerHTML = quotes[nextQuote]; 
 
    nextQuote = (nextQuote + 1) % quotes.length; 
 

 
}
<div id='quote'></div> 
 
<button onClick='newQuote()'>Next Quote</button>

+0

답변 해 주셔서 감사합니다! – NJV

관련 문제