HTML 格式不正确
HTML not formatted properly
我正在尝试从另一个域获取 HTML(例如 Google)。我用this library,用它可以得到HTML:
jQuery(document).ready(function($) {
$.ajax({
url: 'http://google.fr',
type: 'GET',
success: function(res) {
$html = $(res.responseText);
}
});
});
res.responseText
返回的字符串包含所有HTML,但是当我$(res.responseText)
时,没有所有标签,例如我无法获取标题通过 $(res.responseText).find("title")
。我什至尝试了函数 $.parseHTML
,我得到了相同的结果。为什么我的 HTML 没有被正确解析?
你能试试这样吗?
https://jsfiddle.net/omsmvksg/
jQuery(document).ready(function($) {
$.ajax({
url: 'https://google.fr',
type: 'GET',
dataType : 'html',
success: function(res) {
console.log(res.responseText);
$html = $(res.responseText);
console.log($html.find("._yKg").length);
}
});
});
它在 html 下载中记录 _yKg
class 的正确计数。
一切正常,但让我们创建一个假元素:
$html = $("<div />", {html: res.responseText});
$html.find("title"); // This would work.
我正在尝试从另一个域获取 HTML(例如 Google)。我用this library,用它可以得到HTML:
jQuery(document).ready(function($) {
$.ajax({
url: 'http://google.fr',
type: 'GET',
success: function(res) {
$html = $(res.responseText);
}
});
});
res.responseText
返回的字符串包含所有HTML,但是当我$(res.responseText)
时,没有所有标签,例如我无法获取标题通过 $(res.responseText).find("title")
。我什至尝试了函数 $.parseHTML
,我得到了相同的结果。为什么我的 HTML 没有被正确解析?
你能试试这样吗?
https://jsfiddle.net/omsmvksg/
jQuery(document).ready(function($) {
$.ajax({
url: 'https://google.fr',
type: 'GET',
dataType : 'html',
success: function(res) {
console.log(res.responseText);
$html = $(res.responseText);
console.log($html.find("._yKg").length);
}
});
});
它在 html 下载中记录 _yKg
class 的正确计数。
一切正常,但让我们创建一个假元素:
$html = $("<div />", {html: res.responseText});
$html.find("title"); // This would work.