jQuery加载页面的正文

jQuery加载页面的正文,jquery,html,load,get,Jquery,Html,Load,Get,我正在尝试加载页面的正文,如下所示: 但是,在这个线程中,没有人提供有效的解决方案,因为默认情况下,$.load()会切断、和标记(afaik)。我选择了$.get()方法,我已经将页面的全部内容作为字符串获取,但现在我无法仅获取标记(或者更确切地说:标记中包含什么) 到目前为止,我已经尝试: $.get(uri, function(data){ console.log(data); // --> the entire page's content is logged }); $.get

我正在尝试加载页面的正文,如下所示:

但是,在这个线程中,没有人提供有效的解决方案,因为默认情况下,
$.load()
会切断
标记(afaik)。我选择了
$.get()
方法,我已经将页面的全部内容作为字符串获取,但现在我无法仅获取
标记(或者更确切地说:标记中包含什么)

到目前为止,我已经尝试:

$.get(uri, function(data){
console.log(data); // --> the entire page's content is logged
});

$.get(uri, function(data){
console.log($(data)); // --> i guess that's the entire site as an object
});

$.get(uri, function(data){
console.log($(data).find("body")); // --> this should be the <body> tag as an object, but console just outputs "[ ]"
});
$.get(uri,函数(数据){
console.log(数据);//-->记录整个页面的内容
});
$.get(uri,函数(数据){
console.log($(data));//-->我想这就是作为对象的整个站点
});
$.get(uri,函数(数据){
console.log($(data.find(“body”));//-->这应该是作为对象的标记,但console只输出“[]”
});

您可以尝试将HTML数据读取为XML

$.get(uri, function(data){
   console.log($(data).find("body"));
}, 'xml');

jQuery将删除
html
body
标记。例如,在Firebug中:

$(“内容”)

结果:

[div#id000]

在Firebug控制台中单击该按钮可显示以下内容:

<div id="id000">
    <div id="id001">content</div>
</div>
对于loadedContent as:

<html><head><title>title</title></head><body><div id=id000><div id=id001>content</div></div></body></html>
标题内容
这将提供与上述相同的
元素,即未使用
标记。

您尝试过吗

$.get(uri, function(data) {

   console.log('<body>' + data.contents().find('html body').html() + '</body>');

});
$.get(uri,函数(数据){
console.log(“”+data.contents().find('html body').html()+“”);
});
说明 嗯。。让我们看看我是否能正确地演示这一点

是英语的简写

所以当你这么做的时候

$.get(uri, function(data){
    console.log(data); // --> the entire page's content is logged
});
你真的这么做了

$.ajax({
    url: uri,
    type: "GET",
    success: function(msg){
        console.log(msg);
    }
});
默认情况下,它以HTML的形式返回页面。或者,默认情况下,它首先检查页面上的MIME类型,如果没有找到,则返回HTML。如果您想告诉它您想要返回什么,您可以在服务器页面上的MIME类型中执行,也可以使用

如果您希望从请求中以对象的形式返回数据,那么JSON就是一个不错的选择。 代码中唯一真正的区别是

  • $.get()
    替换为
    $.getJSON()

  • $.ajax()中添加
    数据类型:“json”

因此,它可以期望从页面返回JSON数据

现在,您需要做的就是在服务器端准备数据,使用

如果您希望从请求返回对象,这是一种方法


解决方案 除了使用的服务器端修复外,这是解决方案

$.getJSON(uri, function(data){
    console.log(JSON.stringify(data)); 
});

替代解决方案 假设您想保留
$.get()
您只需要

$.get(uri,函数(msg){
var startWith=“”,
以“”结尾;
var iStart=msg.search(startWith);
var iEnd=msg.search(endWith);
msg=msg.substring(iStart+startWith.length,iEnd)
控制台日志(msg);
});

还有一个更高级的答案。

如果你的问题中不包含内嵌html标记,就必须用波浪键共享的单引号将它们括起来。@Mrtherman:这称为“反勾号”。它的位置取决于键盘布局。哇,非常感谢你给出了这个详细的答案,不是这样的!:)(很遗憾,我不能投票支持你,因为我需要更多的声誉:S)@user1122856没问题。我是来帮忙的!:)但如果加载的内容如下所示,则只需标记-->即可:[代码我不想再次加载]。。。它会加载头部和内部的所有内容:/(但我不想再次加载所有脚本)也不起作用(没有任何记录),但感谢您的建议;)
$.getJSON(uri, function(data){
    console.log(JSON.stringify(data));
});
$.ajax({
    url: uri,
    type: "GET",
    dataType: "json",
    success: function(data){
        console.log(JSON.stringify(data));
    }
});
$output = array(
    "msg" => "This is output", 
    "data" => array(
        "info" => "Spaaaace", 
        "cake" => "no"
    ), 
    array(
        "foo", 
        "bar"
    )
);
echo json_encode($output); 
//it will look like this before the text is parsed into JSON in Javascript
//{"msg":"This is output","data":{"info":"Spaaaace","cake":"no"},"0":["foo","bar"]}
$.getJSON(uri, function(data){
    console.log(JSON.stringify(data)); 
});
$.get(uri, function(msg){
    var startWith = "<body>",
        endWith = "</body>";
    var iStart = msg.search(startWith);
    var iEnd = msg.search(endWith);

    msg= msg.substring(iStart+startWith.length, iEnd)
    console.log(msg);
});