使用jQuery解析HTML字符串

3 html jquery

是否可以在HTML字符串上使用jQuery选择器?我正在尝试获取div的html内容,foo.

var code = "<div id='foo'>1</div><div id='bar'>2</div>";
alert( $(code).html( ) ); // returns 1 (not sure how...)
Run Code Online (Sandbox Code Playgroud)

如何在两个不同的语句中获取foo和bar的html内容?

med*_*iev 6

var code = $("<div id='foo'>1</div><div id='bar'>2</div>");

code.each(function() {
    alert( $(this).html() );
})
Run Code Online (Sandbox Code Playgroud)