繁体   English   中英

从外部源加载html

[英]load html from external source

我想在多个页面中包含HTML片段(每个页面都有不同的开发流程和框架)。 为了避免在片段更改时必须更新每个页面,每个页面都正在加载一个当前看起来像这样的Javascript文件

var html = "lots of html with <style> and <script> tags";
var wrapper = document.createElement("div");
wrapper.innerHTML = html;
document.querySelector('body').appendChild(wrapper);

我正在寻找一种更好的解决方案,以使用Javascript从其他来源注入HTML内容。

尝试这个:

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
        <script type="text/javascript">
           $(document).ready(function(){
               $('#selectedTarget').load('path/to/file.html');
           });
        </script>   
    </head>
    <body>
        <div id="selectedTarget">
            Existing content.
        </div>
    </body>
</html>

暂无
暂无

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

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