js文档排序不起作用

js document sort not working

本文关键字:不起作用 排序 文档 js      更新时间:2023-09-26

我正在努力学习JavaScript,我正在关注Michael Moncur JavaScript第4版《24小时》。下面是书中的一个例子,假设将一组名称按顺序排序。但当我点击按钮时,什么也没发生。代码是正确的还是这本书过时了。

名为sort.HTML 的HTML文件

<html>
<head>
<title>Array Sorting Example</title>
<script type="text/javascript" language="javascript" src="sort.js">
</script>
</head>
<body>
<h1>Sorting String Arrays</h1>
<p>Enter two or more names in the field below,
and the sorted list of names will appear in the
text area.</p>
<form name="theform">
Name:
<input type="text" name="newname" size="20">
<input type="button" name="addname" value="Add"
onclick = "SortNames();">
<br>
<h2>Sorted Names</h2>
<textarea cols="60" rows="10" name = "sorted">
The sorted names will appear here.
</textarea>
</form>
</body>
</html>

这是JS文件名sort.JS

// initialize the counter and the array
var numnames=0;
var names = new Array();
function SortNames() {
// Get the name from the text field
thename=document.theform.newname.value;
// Add the name to the array
names[numnames]=thename;
// Increment the counter
numnames++;
// Sort the array
names.sort();
document.theform.sorted.value=names.join(“'n”);
}

任何想法出了什么问题

document.theform.sorted.value=names.join("'n");

问题是你使用的是印刷多宝引号。

如果将替换为",将起作用。