我尝试将和放入外部文件中。在控制台中,代码输出正常,但随后代码返回undefined。
页脚HTML:
<footer id="footer" class="footer mt-auto py-3 bg-light">
<div class="container">
<div class="col-12 text-center">
<a style="text-anchor: middle">© 2021 AlexInCube</a>
</div>
</div>main.html
<script>document.write(importHTMLdata("content/basement.html"))</script>scripts.js
function importHTMLdata(path){
$.get(path, function(html_string)
{
console.log(html_string)
return html_string;
});发布于 2021-09-14 14:57:16
不建议使用document.write -如果在页面加载后执行,它将擦除页面。
只需将此代码放入js文件即可
$(function() { // on page load
const importHTMLdata = path => { // define a function taking a path
$.get(path, function(html_string) { // get the path
$("body")[path.indexOf("footer") != -1 ? "append" : "prepend"](html_string); // if footer call $("body").append, if not call $("body").prepend
});
}
importHTMLdata("content/header.html"); // load header
importHTMLdata("content/footer.html") // load footer
});<script src="scripts.js"></script>https://stackoverflow.com/questions/69180037
复制相似问题