js文件排序不起作用

时间:2015-12-22 11:16:39

标签: javascript html

我正在努力获得JavaScript,而且我正在追踪24周年第4版的Michael Moncur JavaScript。本书中的一个例子是在下面,它假设按顺序对一组名称进行排序。但是,当我点击按钮时,没有任何事情发生。代码是否正确或书籍是否过时。

名为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>
&#13;
&#13;
&#13;

这是JS文件名sort.js

&#13;
&#13;
// 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”);
}
&#13;
&#13;
&#13;

任何想法都错了

1 个答案:

答案 0 :(得分:2)

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

问题是你正在使用印刷的doble引号。

如果将替换为",则可以使用。