我不知道为什么这不起作用。我的意思是据我所知它应该按字母顺序将我的数组打印到 div“输出”
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title> Lexicographic ordering </title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
var words = [];
var input = prompt("Please enter a word or type end to stop prompts");
while (input != 'end') {
words.push(input);
input = prompt("Please enter a word or type end to stop prompts");
}
words.sort();
getElementById('#output').innerHTML= words.join();
</script>
</head>
<body>
<header>Lexicographic Ordering </header>
<hr>
<div class ="page-wrapper">
h1> Lexicographic Ordering </h1>
<div id="output"></div>
</div>
</body>
</html>
最佳答案
您的代码中有两个小错误,它们都在这一行中:
getElementById('#output').innerHTML= words.join();
getElementById
不是 window
的一部分,它是 document
对象的一部分,因此您必须正确引用它。此外,该方法需要一个 ID,而不是一个选择器,因此您不需要在它前面加上 #。
document.getElementById('output').innerHTML= words.join();
那应该做你想做的!或者,由于我注意到您包含了 jQuery,您可以执行 $('#output').innerHTML = ...
来实现相同的效果。
关于javascript - Jscript 不打印到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19193800/