微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

只使用ajax -jquery在body中加载内容

我只想加载页面正文的内容(sample.html)并将其附加到另一个页面的主体(index.html)..怎么能这样做..我是一个菜鸟

我使用下面的代码..但这似乎不能正常工作

$.ajax({
    url: "/site/pages/sample.html",success: function(data){
        $('body').append(data);
    }
});

解决方法

试试:

$('body').load('/site/pages/sample.html body');

The .load() method,unlike $.get(),allows us to specify a portion of
the remote document to be inserted. This is achieved with a special
Syntax for the url parameter. If one or more space characters are
included in the string,the portion of the string following the first
space is assumed to be a jQuery selector that determines the content
to be loaded.

请参阅load()文档中的Loading page fragments

更新:

正如@Windkiller在评论中指出的那样 – 这将复制文档中的body元素.一个更好的解决方案是使用get和替换body的内容,例如:

$.get("/site/pages/sample.html body",function(data) {
     $("body").replaceWith(data);
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐