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

时间:2011-04-08 18:37:18

标签: javascript jquery ajax

我有一个带有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>

1 个答案:

答案 0 :(得分:2)

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

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