【问题标题】:JQuery doesn't load HTML file (in Bootstrap template)JQuery 不加载 HTML 文件(在 Bootstrap 模板中)
【发布时间】:2020-09-10 08:16:55
【问题描述】:

尝试使用 jQuery 加载 Bootstrap 导航。它不起作用...

<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script>
   $(function(){
     $("#nav").load("nav.html");
   });
</script>  
</head>
<body>
  <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top" id="nav">
  </nav>

这里是nav.html - 简化版。而且它没有加载。

<a class="navbar-brand" href="#">Menu</a>

【问题讨论】:

  • 控制台是否出现任何错误?
  • 是的 Uncaught TypeError: $(...).load is not a function at HTMLDocument. (index.html:20) at e (jquery-3.5.1.min.js: 2) 在 t (jquery-3.5.1.min.js:2)
  • 尝试用这个$(document).ready(() =&gt; { $("#nav").load("nav.html"); })替换你的JS
  • 同样的错误...也收到此警告 jQuery.Deferred 异常:$(...).load is not a function TypeError: $(...).load is not a function at HTMLDocument . (file:///Users/andriigorokhovskyi/MyProject/andrii1.github.io/index.html:19:49) at e (code.jquery.com/jquery-3.5.1.min.js:2:30005) at t (code.jquery.com/jquery-3.5.1.min.js:2:30307) undefined
  • @AndriiGorh 您是否尝试将您的jQuery script 放在代码开头的顶部?

标签: javascript jquery jquery-load


【解决方案1】:

我会将其包装到 $(document).ready(function() { ...yourFunction... }); 中,以便在加载文档的其余部分之后执行它

【讨论】:

    【解决方案2】:

    所以,似乎找到了问题所在。我正在使用 Bootstrap 模板,其中包含 jquery-3.5.1.slim.min.js 文件 - 禁用 Ajax

    我将其替换为完整的 jQuery 版本:

    &lt;script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"&gt;&lt;/script&gt;

    现在 .load 似乎可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-20
      • 2011-05-20
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2019-12-19
      • 2016-11-11
      • 2013-05-17
      相关资源
      最近更新 更多