Javascript 从外部JS文件调用HTML内容
我有一个问题,似乎是一个简单的任务。我有一个网页,当用户单击按钮时,我需要在其中加载和删除div内容。但我的代码似乎不起作用 html:Javascript 从外部JS文件调用HTML内容,javascript,html,Javascript,Html,我有一个问题,似乎是一个简单的任务。我有一个网页,当用户单击按钮时,我需要在其中加载和删除div内容。但我的代码似乎不起作用 html: <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Main Page</title> <link rel="stylesheet" href="assets/css/
<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>")
}
函数第2部分(){
文件。写(“
这是第二个触发器。它应该在左边
下一行到这里
")
}
它在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>");
}
函数第2部分(){
document.write(“\n这里是第二个触发器。它应该在左边\n下一行在这里\n”);
}
稍微干净一点的解决方案是使用单引号和双引号的组合:
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>'
);
}
函数第2部分(){
文件。写('\
\
这是第二个触发器。它应该在左边\
下一行在这里\
'
);
}
请注意,如果要使用一个分为多行的字符串,则必须在每行末尾添加“\”,以让JS解析器知道该字符串将继续到下一行。有效。我不知道“\”这个词。我对这个很陌生。谢谢我现在遇到的另一个问题是,它将脚本中的内容写在一个空白的白色页面上,忽略任何css。有没有一种方法可以让我将html写入当前页面并让它读取样式?