Ajax替换整个页面,而不仅仅是div



我有一个带有Ajax调用的页面,该页面应该替换内容div。但是,当单击链接时,整个页面被内容div替换,而不仅仅是替换它应该替换的文本。任何想法吗?

$(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>

为什么不直接使用$.load()呢?

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

相关内容

最新更新