我试图用按钮对id =“demo”的内容进行排序,但每次点击我的按钮时我什么也得不到。排序数组的错误
点击按钮后,我希望select()能够读取id =“demo”(一旦随机数字输出)的内容并将它们排序/转储到id =“demo2”中。
我认为正在发生的是选择()正在读VAR改编为空白阵列,而不是数字的随机分配的数组(因为它是在第一个()结束)
<input id="input1" type="number" min="10" max="100" onchange="first(); sortButton();">
<p id="demo"></p>
<!-- button appears here, once a value is entered into the input field -->
<p id="buttons1" onclick="select();"></p>
<p id="demo2"></p>
<script>
// once input1 value changes this outputs a random value less than N (value of input1) N times, then dumps out the random numbers in id="demo"
function first() {
var N = document.getElementById("input1").value;
var arr = [];
while(arr.length < N)
{var randomnumber = Math.ceil(Math.random()*N);
arr[arr.length] = randomnumber;}
document.getElementById("demo").innerHTML = arr;}
// Once input1 value changes, this buttons appears in id="buttons"
function sortButton() {document.getElementById("buttons1").innerHTML =
'<button type="button" onclick="select();">Select Sort</button>';}
// meant to sort the random numbers in id="demo" once the button is clicked
function select() {
document.getElementById("demo2").innerHTML = arr.sort(function(a,b){return a - b});}
</script>
是您的代码只是没有那么好格式化?我几乎会认为'select'是'first'函数的一部分,当你运行代码时会发生什么变化,你在调试器中是否有错误?单击按钮时是否显示错误? – Icepickle