javascript - js 文档排序不起作用

标签 javascript html

我正在努力学习 JavaScript,并且正在关注 Michael Moncur JavaScript 的 24Hr 第 4 版。下面是书中的一个示例,它假设对一组名称进行排序。但是当我点击按钮时什么也没有发生。代码是否正确,或者这本书是否已过时。

名为 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");

问题在于您使用的是打印双引号。

如果将 替换为 即可。

关于javascript - js 文档排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34414639/

相关文章:

javascript - svg/snapsvg 创建弯曲的文本

javascript - 输入分数并计算平均值

html - 防止渐变叠加滚动

javascript - 如何在 React js 0.13.3 中访问嵌套对象

javascript - 如何启用iframe的设计模式?

javascript - React - 检查元素在 DOM 中是否可见

javascript - Math.log10 在 PhantomJS 中未定义/可用

javascript - jQuery() 和 $() 未定义,但 jQuery 工作正常

html - Div 向左浮动向下

php - 为什么我在 Opera Mobile 中打开网页时会放大?