简体   繁体   English

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

[英]Ajax replacing entire page, not just the div

I have a page with an Ajax call that is supposed to replace a content div. 我有一个Ajax调用页面应该替换内容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. 但是,当单击链接时,整个页面将替换为内容div,而不是仅替换它应该使用的文本。 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 HTML

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

Why don't you just use $.load()? 你为什么不用$ .load()?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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