如何檢查變量是否等於數組中的任何元素?如何匹配數組的任何元素?
var myButton = document.getElementById("myButton");
var myVar; //myVar value is set to "One", "Two" or "Three" sometime later
myArray = ["One","Two","Three"];
myButton.onclick = function() {
if (myVar === myArray) {
alert ("it's a match!");
} else {
alert ("it's not a match!");
}
};
最簡單,最有效的是循環。如果你想要一些奇特的東西看看http://stackoverflow.com/questions/890782/javascript-function-inarray –
你必須循環,但你可以'打破',在第一場比賽,所以你不需要繼續循環。 –