jQuery $.ajax() 只获取正文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3861325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:14:35  来源:igfitidea点击:

$.ajax() get body only

jqueryhtmlajax

提问by GusDeCooL

i have script like this

我有这样的脚本

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Testing Ajax</title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<a class="test" href="getthis.php">click here</a>
<div class="get"></div>

<script type="text/javascript">
    $('.test').click(function(event){
        event.preventDefault();
        var a = $('body');
        $.ajax({
            url: "/getthis.php",
            dataType: 'text',
            success: function(data){            
                $('.get').append(data.find);
            }
        });
    });
</script>
</body>
</html>

with this script i try to get content getthis.php

使用此脚本,我尝试获取内容 getthis.php

getthis.php contains only this

getthis.php 只包含这个

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title></title>
    </head>
    <body>
        Olalalalala bebe
    </body>
    </html>

when i do this i get result the full html of the getthis.php

当我这样做时,我得到了 getthis.php 的完整 html

how i can to only get body content? which is mean only. "Olalalalala bebe"

我怎样才能只获得正文内容?这只是意思。《奥拉拉拉拉贝贝》

can somebody give me explanation?

有人可以给我解释一下吗?

Thanks...

谢谢...

回答by jknair

use http://api.jquery.com/load/you can load specific page fragments

使用http://api.jquery.com/load/你可以加载特定的页面片段

回答by user1709374

Pretty easy. Wrap inner of BODY by one more layer of DIV will do:

挺容易。将 BODY 的内部再包裹一层 DIV 将执行以下操作:

$.get(url, {}, function(data){
  var data = data.replace('<body', '<body><div id="body"').replace('</body>','</div></body>');
  var body = $(data).filter('#body');
  //... do what you want with body ...
});

回答by Salman A

Wondering if this helps:

想知道这是否有帮助:

$(data).text();

This seems to work out of the box:

这似乎是开箱即用的:

$.ajax({
    url: 'http://stackoverflow.com/questions/3861325/ajax-get-body-only/',
    success: function (data) {
        alert($(data).text().replace(/\s+/gm, ' '));
    }
});

回答by Mark Bell

This should do it:

这应该这样做:

$('.get').load('/getthis.php body');

Edit:Sorry, I just tested this and it doesn't seem to work using bodyas a selector, which surprises me. If I change getthis.phpto this:

编辑:抱歉,我刚刚测试了这个,它似乎不能body用作选择器,这让我感到惊讶。如果我改成getthis.php这样:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title></title>
    </head>
    <body>
        <div id="test">
        Olalalalala bebe
        </div>
    </body>
</html>

And call it like this:

并这样称呼它:

$('.get').load('/getthis.php #test');

I get the result you are looking for. But it seems bodyis not a valid selector in this case.

我得到了你正在寻找的结果。但body在这种情况下它似乎不是一个有效的选择器。

回答by Chinmayee G

If not required you exclude html tags from your getthis.php have that data which you want to display on page.

如果不需要,您可以从 getthis.php 中排除 html 标签,以便在页面上显示这些数据。

回答by Bryan Smith

$.ajax({
    url: "some.html",
    type: "get",
    dataType: 'html',
    success: function(data) {
        $('#here').html(data);
        $('#here').children('meta,link,title,style').remove();
    },
});