我正在尝试从我加载到$(数据)的远程页面中选择一个h1。 无论我如何尝试,我似乎无法让代码正确。 如果我使用此代码:
console.log($(data));
我明白了:
[text, meta, text, meta, text, script, text, title, style, text, style, text, h1, text, h2, text, br, text, br, text, table, text, hr, init: function, selector: "", jquery: "1.4.2", size: function, toArray: function…]
在索引12处是我想要获取的内容。但是怎么样? 我认为这样可行,但它什么都不返回:
console.log($(data).find('h1').text());
如果我删除text(),我会得到:
[prevObject: c.fn.c.init[23], context: undefined, selector: "h1", init: function, jquery: "1.4.2"…]
context: undefined
length: 0
prevObject: c.fn.c.init[23]
0: text
1: meta
2: text
3: meta
4: text
5: script
6: text
7: title
8: style
9: text
10: style
11: text
12: h1
13: text
14: h2
15: text
16: br
17: text
18: br
19: text
20: table
21: text
22: hr
length: 23
__proto__: Object[0]
selector: "h1"
__proto__: Object[0]
再次,在索引12处。我错过了什么/不明白?
最诚挚的问候 尼克拉斯
答案 0 :(得分:0)
$('<p></p><a>One</a>').find('a').text();
Outputs: ""
$('<p><a>Two</a></p><a>One</a>').find('a').text();
Outputs: "Two"
请注意,<a>Two</a>
是<p>
的后代。我认为你需要的是过滤器。 Reduce the set of matched elements to those that match the selector or pass the function's test.
$('<p></p><a>One</a>').filter('a').text();
Outputs: "One"