使用jQuery将HTML文件包含在另一个HTML文件中时遇到错误

时间:2018-07-08 10:46:41

标签: jquery html css

我正在使用此代码,当我在浏览器中运行此代码时,我得到一个空白页,请帮帮我,谢谢。

<html> 
  <head> 
    <script src="js/jquery-3.3.1.js"></script>
    <script> 
    $(function(){
      jQuery.noConflict();
      $("#includedContent").load("a.html"); 
    });
    </script> 
  </head> 

  <body> 
     <div id="includedContent"></div>
  </body> 
</html>

我的 a.html 正在关注

<html>
    <p>this is included file</p>
</html>

4 个答案:

答案 0 :(得分:2)

您不能在彼此之间嵌套html标记。最好的解决方案是改用iframe。

$("#includedContent").html("<iframe src='a.html'></iframe>"); 

答案 1 :(得分:0)

enter image description here

出于安全原因,浏览器不允许使用涉及XHR / AJAX的方法加载文件。

$("#includedContent").load("a.html");

您需要在服务器上托管/部署文件,请尝试使用XAMPP / WAMPP或同等版本。

enter image description here

答案 2 :(得分:0)

如果要加载特定的选择器,则应使用load(),但如果要使用get()加载整个页面,请在此SO中查看jQuery.get()之间的区别和jQuery.load();

$.get('a.html', appndHtml);
function appndHtml(data){
   $("#includedContent").prepend(data);
}

答案 3 :(得分:0)

 $(document).ready(function() { 
   $('#some-container').load('path-name.html'); 
 });

文档应准备就绪或已完全加载。