从外部JS文件调用HTML内容
Calling HTML content from external JS file
我遇到了一个看似足够简单的任务的问题。我有一个网页,当用户单击按钮时,我需要在其中加载和删除div内容。不过,我的代码似乎不起作用。
html:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Main Page</title>
<link rel="stylesheet" href="assets/css/stylemain.css"/>
<script src="assets/js/introduction.js"></script>
</head>
<body>
<div id="container">
<div id="content">
<div id="slide1">
<p>Here is the first trigger. It should look something like this</p>
<p><input type="button" onclick="part2()" value="Click Me!" /></p>
</div>
</div>
和.js文件:
function part2() {
document.write("<div id="slide2">
<p>Here is the second trigger. It should be to the left</p>
<p>next line goes here</p>
</div>")
}
它在js文件的第2行(document.write行)出现了语法错误,但我不知道为什么。我试过引用和不引用,但都没有用。如有任何帮助,我们将不胜感激。
您必须转义引号:
function part2() {
document.write("<div id='"slide2'">'n<p>Here is the second trigger. It should be to the left</p>'n<p>next line goes here</p>'n</div>");
}
一个稍微干净一点的解决方案是使用单引号和双引号的组合:
function part2() {
document.write(''
<div id="slide2">'
<p>Here is the second trigger. It should be to the left</p>'
<p>next line goes here</p>'
</div>'
);
}
请注意,如果您想使用一个分为多行的字符串,则必须在每行的末尾添加"''",以使JS解析器知道该字符串将继续到下一行。
相关文章:
- 如何根据html内容使用ng类-AngularJS
- JavaScript在其他页面上用html内容填充文本框
- Rails:如何在浏览器中显示动态html内容,而不会对其进行转义,也不会引起XSS攻击
- 使用javascript动态创建html内容/元素
- 无法将大型html内容POST到Chrome上的服务器
- 使用li元素的html内容更改该元素的背景
- 如何从ajax返回两个html内容
- jQuery获取子DIV的HTML内容
- HTML内容下推到固定背景图像下方
- 在angular中,使用ng repeat生成动态html内容
- 使用$watch更改html内容
- 如何使用window.showModalDialog()显示HTML内容
- 如何在使用 UIWebView 编辑 HTML 内容时正确调整输入语言更改 (LTR/RTL) 的文本对齐方式
- 获取垂直溢出容器的 html 内容
- 为数组中的每个项目追加 html 内容
- Dojo:在 iframe 加载时解析 HTML 内容
- 允许 Itemp 在 Enyo 中使用 HTML 内容
- 从选项卡中获取 HTML 内容
- 可以将HTML内容放在画布上(登录页)
- 只有当类是硬编码的时,才可以使用jQuery来显示/隐藏html内容