I'm tempering with a Chrome Extension where I use an Ajax-request to get HTML from a requested URL. This works, but I want to get all the text values some certain elements. By example, everything with the class .heading-bold
script.js
我正在使用Chrome扩展程序,我使用Ajax请求从请求的URL获取HTML。这有效,但我希望得到一些特定元素的所有文本值。例如,所有类都带有.heading-bold script.js
$.ajax({
url: "http://page.com/page.html",
type: "GET",
dataType: "html",
success: function(data) {
console.log($(data).filter( '.heading_bold' ).text());
}
});
Response HTML
响应HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Beerpong</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
</head>
<body>
<div id="table-container">
<table>
<tbody>
<tr>
<td><div class="heading_bold">Beerpong</div></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Logging it to the console works just fine. This is my output:
将其记录到控制台工作正常。这是我的输出:
Uncaught Error: Syntax error, unrecognized expression: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0....
Why? Why won't it just console.log my desired values?
为什么?为什么不只是console.log我想要的值?
2 个解决方案
#1
16
If you are using jquery 1.9, do:
如果您使用的是jquery 1.9,请执行以下操作:
...
success: function(data) {
var html = $.parseHTML(data);
console.log($(html).find( '.heading_bold' ).text());
}
..
Because as per jQuery 1.9:: HTML strings passed to jQuery() that start with something other than a less-than character will be interpreted as a selector. Since the string usually cannot be interpreted as a selector, the most likely result will be an "invalid selector syntax" error thrown by the Sizzle selector engine. Use jQuery.parseHTML() to parse arbitrary HTML.
因为根据jQuery 1.9 ::传递给jQuery()的HTML字符串以不同于小于字符的东西开头将被解释为选择器。由于字符串通常不能被解释为选择器,因此最可能的结果是Sizzle选择器引擎抛出的“无效的选择器语法”错误。使用jQuery.parseHTML()来解析任意HTML。
#2
1
Maybe you could try to use "load" instead of "$.get()" if you want to insert a portion of the remote document into DOM.
如果要将远程文档的一部分插入DOM,也许可以尝试使用“load”而不是“$ .get()”。
$("#result").load("page.html .heading_bold",function(response){
console.log($(this).find(".heading_bold").val());
});
Hope this is helpful to you.
希望这对你有所帮助。
#1
16
If you are using jquery 1.9, do:
如果您使用的是jquery 1.9,请执行以下操作:
...
success: function(data) {
var html = $.parseHTML(data);
console.log($(html).find( '.heading_bold' ).text());
}
..
Because as per jQuery 1.9:: HTML strings passed to jQuery() that start with something other than a less-than character will be interpreted as a selector. Since the string usually cannot be interpreted as a selector, the most likely result will be an "invalid selector syntax" error thrown by the Sizzle selector engine. Use jQuery.parseHTML() to parse arbitrary HTML.
因为根据jQuery 1.9 ::传递给jQuery()的HTML字符串以不同于小于字符的东西开头将被解释为选择器。由于字符串通常不能被解释为选择器,因此最可能的结果是Sizzle选择器引擎抛出的“无效的选择器语法”错误。使用jQuery.parseHTML()来解析任意HTML。
#2
1
Maybe you could try to use "load" instead of "$.get()" if you want to insert a portion of the remote document into DOM.
如果要将远程文档的一部分插入DOM,也许可以尝试使用“load”而不是“$ .get()”。
$("#result").load("page.html .heading_bold",function(response){
console.log($(this).find(".heading_bold").val());
});
Hope this is helpful to you.
希望这对你有所帮助。