开发者

What can be used as [context] in jQuery( expression, [context] )?

开发者 https://www.devze.com 2022-12-21 00:41 出处:网络
The following code does not return anything: $(\'.foo\', $(\'<di开发者_如何学Pythonv class=\"foo\">foo</div><div class=\"bar\">bar</div>\').get(0)).html()

The following code does not return anything:

$('.foo', $('<di开发者_如何学Pythonv class="foo">foo</div><div class="bar">bar</div>').get(0)).html()

Is there any way to specify a string representation of xml/html as the context parameter of the jQuery function? I'm trying to select a section of an html document retrieved from an ajax request; the ajax callback (using $.get(), for example) provides the returned data -- in this case html.


The problem in your example is that your context is two sibling elements. Using a wrapper element will allow you to select things inside it. Notice the difference here:

$('.bar', $('<div class="foo"><div class="bar">bar</div></div>')).html(); // outputs "bar"
0

精彩评论

暂无评论...
验证码 换一张
取 消