【发布时间】:2022-01-16 14:25:09
【问题描述】:
我使用 nodejs(在 docker-compose 容器中,不确定是否相关)来设置本地 Web 应用程序。我想在客户端使用 jquery 将 HTML 文件加载到 div 中。不幸的是,它不起作用。初始 index.html 和 new.html 位于同一文件夹 (/frontend/) 中。我错过了什么?
Nodejs 应用:
var app = express();
// home route returns rendered html content
app.get("/", (req, res) => {
res.sendFile(__dirname + "/frontend/index.html");
});
app.listen(3000, function(){
console.log('Example app listening on port 3000!');
});
index.html:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#imported").load("new.html");
});
</script>
</head>
<body>
<div> This is content of the index HTML and below is the imported HTML:</div>
<div id="imported"></div>
</body>
</html>
最后是我要导入new.html的HTML:
<HTML>
<head>
</head>
<body>
<p>This is demo text.<p>
</body>
</html>
【问题讨论】:
标签: javascript html jquery node.js express