JavaScript文档.write不工作
JavaScript document.write is not working
很抱歉,如果这看起来很愚蠢,我是JavaScript新手。
这是在menu.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
:中
<head>
</head>
<body>
<script type="text/javascript" src="menu.js"></script>
</body>
</html>
当我加载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('<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()
的单个调用,如下所示:
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>");
否则,JavaScript认为字符串在href=
之后结束,并且该行的其余部分不遵循有效的JavaScript语法。
正如@Felix所提到的,JavaScript调试器工具将非常有助于让您了解发生了什么。
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- document.write,它是如何工作的
- JavaScript文档.write不工作
- Javascript-document.write()正在工作,但document.innerHTML不起作用;t
- 为什么我的javascript for循环将不工作?For (var i=1;i< 5;i++;){文档.write (
- Javascript不工作,我需要帮助.有人建议我不要使用document.write
- 返回语句不工作,使用document.write
- document.write()和Ajax -不工作,寻找替代方案
- 清理完文档后,document.write(ln)不再工作
- document.write()内联工作,但不使用jQuery's .html()
- 为什么我的函数只工作一次(做一个document.write()作业)
- 有人能解释一下为什么文档["write"]工作
- 文档.Write不能工作,但是console.log可以