简体   繁体   中英

Ajax replacing entire page, not just the div

I have a page with an Ajax call that is supposed to replace a content div. When the link is clicked, though, the entire page is replaced with the content div, instead of it just replacing the text it was supposed to. Any thoughts?

$(document).ready(function () {
    $('#gnav a').click(function () {
        var page = this.hash.substr(1);
        $('#contents').html("");
        $('#content_wrapper').block();

        $.get(page +'.php', function(gotHtml){
            $('#contents').html(gotHtml);
            $('#content_wrapper').unblock();
        });
        return false;
    });
});

HTML

<div id="contents" style="height:1200px; width: 620px">
    <!-- all html here that should be replaced-->
</div>

Why don't you just use $.load()?

$(function () {
    $('#gnav a').click(function (e) {
        e.preventDefault();
        $('#contents').load(this.hash.substr(1) +'.php')
    });
});

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