简体   繁体   English

使用 JQuery 从外部文件加载 div

[英]Load div from external file using JQuery

I'm running into a problem with JQuery, I've read multiple threads about this but I cannot for the life of me figure out what is wrong with my code.我遇到了 JQuery 的问题,我已经阅读了多个关于此的线程,但我无法终生弄清楚我的代码有什么问题。 I've gotten it down to a pretty simplistic version of what I was trying to do, but it still will not work.我已经把它归结为我试图做的一个非常简单的版本,但它仍然行不通。 Here is the code that I'm trying:这是我正在尝试的代码:

<!DOCTYPE html>
<html>
    <head>
        <script src="jquery-3.6.0.min.js"></script>
        <script>
            $(function() {
                $('#content').load("import.html");
            });
        </script>
    </head>
    <body>
        <div id="content"></div>
    </body>
</html>

and here is the page that I'm trying to import:这是我要导入的页面:

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <div id="test-div">
            <p>This is some text.</p>
        </div>
    </body>
</html>

can somebody please point out what I might be doing wrong here?有人可以指出我在这里可能做错了什么吗?

The DOM is likely not fully loaded when the.load function is being run.运行 .load function 时,DOM 可能未完全加载。 Change to this.改成这个。

<script>
$(document).ready(function() {
  $('#content').load("import.html");
});
</script>

Note - as in the comment, this only works on a server.注意 - 如评论中所述,这仅适用于服务器。

It should ideally work.它应该理想地工作。 There must be some other issue应该还有其他问题

  1. Check if jquery library file is loading correctly检查 jquery 库文件是否正确加载
  2. HTML page name is import.html or not. HTML 页面名称是否为 import.html。
  3. At last, Are you getting any error on console?最后,您在控制台上收到任何错误吗?

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

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