JavaScript document.write不工作

JavaScript document.write不工作,javascript,document.write,Javascript,Document.write,抱歉,如果这看起来很愚蠢,我是JavaScript新手 这在菜单.js中: document.write("<a href="index.html">Home</a>"); document.write("<a href="news.html">News</a>"); document.write("<a href="about.html">About us</a>"); 当我加载index.html时,什么也没有出现…问

抱歉,如果这看起来很愚蠢,我是JavaScript新手

这在
菜单.js中:

document.write("<a href="index.html">Home</a>");
document.write("<a href="news.html">News</a>");
document.write("<a href="about.html">About us</a>");

当我加载
index.html
时,什么也没有出现…

问题在于你的引号,你正在使用
来界定你的新元素并设置它们的
href
属性,将你的代码改为:

document.write("<a href='index.html'>Home</a>");
document.write("<a href='news.html'>News</a>");
document.write("<a href='about.html'>About us</a>");

您没有转义字符串中的引号。它应该是:

document.write("<a href=\"index.html\">Home</a>");
document.write(“”);
否则,JavaScript认为字符串在
href=
之后结束,该行的其余部分不遵循有效的JavaScript语法


正如@Felix所提到的,JavaScript调试器工具将非常有助于让您知道发生了什么。

并且您将知道问题所在。或者更好的是,使用一个编辑器为您突出显示这些错误,以便在您测试之前就捕获它们。
document.write('<a href="index.html">Home</a>');
document.write('<a href="news.html">News</a>');
document.write('<a href="about.html">About us</a>');
document.write('<a href="index.html">Home</a>' 
    + '<a href="news.html">News</a>'
    + '<a href="about.html">About us</a>');
document.write("<a href=\"index.html\">Home</a>");