简体   繁体   中英

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. 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. 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
  2. HTML page name is import.html or not.
  3. At last, Are you getting any error on console?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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