加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 02:46:03 所属栏目:百科 来源:网络整理
导读:我只想加载页面正文的内容(sample.html)并将其附加到另一个页面的主体(index.html)..怎么能这样做..我是一个菜鸟 我使用下面的代码..但这似乎不能正常工作 $.ajax({ url: "/site/pages/sample.html",success: function(data){ $('body').append(data); }});
我只想加载页面正文的内容(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);
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读