简体   繁体   English

如何在网页中显示外部HTML页面-使用JavaScript

[英]How display an external html page in a webpage - using javascript

I need to display an existing html page in another page. 我需要在另一个页面中显示现有的html页面。 The object> tag displays it inside an iframe - which I don't want. object>标记将其显示在iframe中-这是我不想要的。

I read here: https://stackoverflow.com/questions/17148357/including-external-html-file-to-another-html-file 我在这里阅读: https : //stackoverflow.com/questions/17148357/includes-external-html-file-to-another-html-file

That it can be done with this piece of javascript: 可以用这段javascript完成:

<script type="text/javascript">
$(document).ready(function(e) {
    $('#header').load('name.html',function(){alert('loaded')});
});
</script>

Before this I've inserted: 在此之前,我已经插入:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

But I'm getting the error: DOMException: failed to execute 'querySelectorAll' on element 但是我遇到了错误: DOMException:无法在元素上执行“ querySelectorAll”

What have I missed? 我错过了什么?

PS. PS。 I tried the document.write+="html+javascript-goes-here" method, but the external page-content is large so the document.write is long, cumbersome, and thus difficult to later understand/maintain. 我尝试了document.write + =“ html + javascript-goes-here”方法,但是外部页面内容很大,因此document.write很长,很繁琐,因此以后很难理解/维护。

在此处输入图片说明

Thanks stackOverflow - Got it running, once I'd spotted the "$ is not defined" error msg. 谢谢stackOverflow-让它运行,一旦我发现“ $未定义”错误味精。 Surrounded my function with: 围绕我的功能:

(function($){
.... function here ...
})(jQuery);

and: 和:

<div id="header"></div>

in the body. 在身体里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM