2
我的codepen。我想輸出一條消息,如果我從數組中獲取需要的元素。如果我需要返回消息數組元素
的JavaScript:
function GetValue()
{
var myarray = new Array("word1","word2","word3","word4","word5");
var random = myarray[Math.floor(Math.random() * myarray.length)];
document.getElementById("message").innerHTML=random;
};
//rand array element
var clicks = 0;
function onClick() {
clicks += 1;
document.getElementById("clicks").innerHTML = clicks;
};
//count clicks
function printMessage(random, clicks) {
if (random === "word1") {
document.getElementById("alert").innerHTML =
"you get " + random + "after " + clicks + "clicks";
}
}
//display "you get word1 after _ clicks"
你會想從裏面的GetValue調用printMessage,這樣你就可以從陣列通過它的隨機字。如果您還想要使用點擊量,請將onClick函數合併到GetValue函數中,或者讓另一個函數調用另一個函數。你在html onclick =「GetValue(); onClick」中調用了兩個單獨的函數,所以如果你要合併它們或者讓另一個函數調用另一個函數,你可以使用點擊作爲參數。 – Shilly