How to select elements stored in a string variable?
if I have a valid html fragment in a variable like so;
var responseText = 'Lorem <span>Ipsum</span>';
How can I find the $("span").html()
of that responseText
, in other words get 开发者_如何学运维the Ipsum?
Usually, if responseText
represented an HTML element, you could have used $(responseText)
to parse it. Here, it easiest to create a temporary element, and setting its html
:
var dummy = $('<div />').html(responseText);
var text = dummy.find('span').text();
Working example: http://jsbin.com/ebeju4
$('<div/>').html(responseText).find('span').html();
精彩评论