任何人都可以在下面的JavaScript代码中帮助我吗?我不能够得到输出
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
var arr = new Array(1, 10, 8, 56, 12, 45);
document.getElementById("srt").innerHTML = arr;
function asc() {
arry.sort(function(a, b) {
return a - b;
})
document.getElementById("srt").innerHTML = arr;
}
function desc() {
arry.sort(function(a, b) {
return b - a;
})
document.getElementById("srt").innerHTML = arr;
}
</script>
</head>
<body bgcolor="aqua">
<div>
<h4>Click the button to sort the Array.</h4>
</div>
<div><input type="button" onclick="asc()" value="Ascending"></div>
<div><input type="button" onclick="desc()" value="Descending"></div>
<div>
<h4><span id="srt"></span></h4>
</div>
</body>
</html>
请检查我的javascript code.I想我会错了地方不对劲的地方。当我点击按钮排序的数组没有得到显示。
问题是您在尝试访问元素之前,它呈现。尝试包装你的代码加载('window.addEventListener('加载',函数(){//你的代码在这里})')事件或加载资源末尾 – Rajesh
也有一个错字。 'arry.sort'?它应该是'arr.sort' – Rajesh
没有这个更正和答案@Rajesh lol :) :) – user7417866