gpt4 book ai didi

javascript - 从外部JS文件调用HTML内容

转载 作者:行者123 更新时间:2023-12-03 12:45:18 24 4
gpt4 key购买 nike

我对看似简单的任务遇到了问题。我有一个网页,需要在用户单击按钮时加载和删除 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>");
}

关于javascript - 从外部JS文件调用HTML内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com