简体   繁体   中英

Using jQuery to load external PHP page

I'm using this code to load pages onclick without reloading the current page:

$(function() {
   $("#tabs a").click(function() {
      var page = this.hash.substr(1);
      $.get(page+".php", function(html) {
         $('#content').html(html)
      });
      return false;
   });
});

The code is working and everything's fine except that it looks for the pages to display within the same path of the current page, for example if the current page is www.mysite.com/index It looks for www.mysite.com/index/page.php and this page doesn't exist because I put my PHP pages in a specific folder how to change the code to make it look for the external pages in the specific folder that I want?

.get , forms, anchors, etc. will all use relative paths if you give them relative paths. If you want them to use an absolute path, provide an absolute path. Absolute paths begin with a slash / .

For example:

$.get("/" + page + ".php" ...

This of course requires page to be the full path.

Try this: $('#content').html(html); You forgot parentheses around the jQuery selector.

$("#tabs a").click(function() {
    var page = this.hash.substr(1);
    $.get("php/" + page + ".php", function(html) {
        $('#content').html(html);
    });
    return false;
});

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