我正在尝试创建一个二维数组 - 但是在JS控制台中它一直说我的“Randarray”函数没有定义。我似乎无法弄清楚为什么它没有定义,也许我只需要另外一双眼睛来看它。
感谢任何帮助!
extra-tmp-files
答案 0 :(得分:2)
您忘记在Randarray
功能之前关闭getArray
功能。这样做:
} // Missing.
function getArray(row,col)
此外,不要使用事件监听器以及onclick
内联函数。
<button id="array" onclick="Randarray()">Array</button>
<script>
function Randarray() {
randarray = new Array(5)
randarray[0] = new Array(3)
randarray[0][0] = "Toyota"
randarray[0][1] = "1998"
randarray[0][2] = "Black"
randarray[1] = new Array(3)
randarray[1][0] = "Ferrari"
randarray[1][1] = "2006"
randarray[1][2] = "Red"
randarray[2] = new Array(3)
randarray[2][0] = "Ferrari"
randarray[2][1] = "2006"
randarray[2][2] = "Red"
randarray[3] = new Array(3)
randarray[3][0] = "Jeep"
randarray[3][1] = "2006"
randarray[3][2] = "Silver"
randarray[4] = new Array(3)
randarray[4][0] = "Mercedes"
randarray[4][1] = "2016"
randarray[4][2] = "White"
randarray[5] = new Array(3)
randarray[5][0] = "BMW"
randarray[5][1] = "2017"
randarray[5][2] = "Black"
}
function getArray(row, col) {
document.arrayForm.myResult.value = randarray[row][col];
}
document.getElementById('array').addEventListener('click', Randarray);
</script>