我正在为学校创建一个简单的数组计数器和分类器,并且遇到了一个调用count()函数的Count按钮的问题。这是我的代码。
<!DOCTYPE html>
<html>
<head>
<script language=javascript>
var products = ["Printer","Tablet","Router","Keyboard","Headset"];
function sort() {
products.toString()
document.getElementById("result").innerHTML = "Unsorted array = " + products;
products.sort();
products.toString();
document.getElementById("result2").innerHTML = "Sorted array = " + products;
}
function count() {
document.getElementById("count").innerHTML = "The amount of items in the array is: " + fruit.length;
}
</script>
<title>Task 2</title>
</head>
<body>
<input type="button" name="butt1" value="Sort!" onclick="sort()"/>
<input type="button" name="butt2" value="Count!" onclick="count()"/>
<p id="result"></p>
<p id="result2"></p>
<p id="count"></p>
</body>
</html>
答案 0 :(得分:1)
这是因为fruit
对于计数功能
我想你想在这里使用products
function count() {
document.getElementById("count").innerHTML = "The amount of items in the array is: " + products.length;
}
或者如果您想使用fruit
,那么您需要全局声明并初始化