2013-10-07 57 views
1

我是一個學生,所以我詢問這樣一個簡單的問題道歉。我試圖做一個「下一個和‘上一個’按鈕,通過陣列一次一個循環。循環通過一個按鈕陣列,鈦

var food = ["pasta", "Salad", "Apple", "Pizza"]; 
    counter=0; 

    var displayAlert = function(){ 
     for (var i=0, item; i<food.length; i++) { 
     item = food[i]; 
     quoteText.text=food[i]; 
} 
};  

    var quoteView = Ti.UI.createView({ 
    backgroundColor: "#fff", 
    height: 150, 
    top: 50, 
    left: 20, 
    right: 20, 
    borderRadius: 5 

}); 


var quoteText = Ti.UI.createLabel({ 
    text: "click below to begin", 
    font: {fontSize: 20, fontFamily: "Arial"}, 
    textAlign: "center" 
}); 
    quoteView.add(quoteText); 
    mainWindow.add(quoteView); 



buttonPrevious.addEventListener("click", displayAlert); 
buttonNext.addEventListener("click", displayAlert); 
+0

沒有必要道歉。你到底是想要做什麼?你想輸出上的每個按鈕全陣列式點擊嗎?你要輸出的下一首/上點擊按鈕上的數組的項目? – mwfire

回答

0

所以我已經通過代碼與我的教練工作,但我會採取零這個任務,別人可能會受益於我的不幸。

var food = ["pasta", "Salad", "Apple", "Pizza"]; 
    counter=0; 

    var displayAlert = function(){ 
     console.log (counter); 
    quoteText.text=food[counter]; 
    if (counter === food.length-1){ 
     counter=0; 
    } 
    else {counter=counter+1}; 
};  
    var getPrevious = function(){ 
     quoteText.text=food[counter]; 
    if (counter === 0){ 
     counter=food.length-1; 
    } 
    else {counter=counter-1}; 

}; 
    var quoteView = Ti.UI.createView({ 
    backgroundColor: "#fff", 
    height: 150, 
    top: 50, 
    left: 20, 
    right: 20, 
    borderRadius: 5 

}); 


var quoteText = Ti.UI.createLabel({ 
    text: "click below to begin", 
    font: {fontSize: 20, fontFamily: "Arial"}, 
    textAlign: "center" 
}); 
    quoteView.add(quoteText); 
    mainWindow.add(quoteView);