jQuery. Treat a string as a HTML document?
I'm not sure if I worded the title correctly.
Basically is it possible to treat a string as HTML as if it was on the page? So I can use $("#elem") and all the other jQuery functions?
The HTML is loaded into a str开发者_运维百科ing from an ajax request and stored in a string. Instead of using regular expressions to access the data needed is it possible to use jQuery functions?
ajaxTextResponse.$("#telephone");
I know the above won't work, but you see what I am getting at.
Thanks
If you feed HTML in the form of a string jQuery will do its best to parse it:
success:function(data) {
$('#el', data).appendTo('body')
}
An example of extracting #el when it's a child of some other element.
Yes, you can use a string as HTML, rather than loading it on the page, though, you might want to store it in a div first, before you work with it, it sounds like. So, try something like this:
$.post(url, data, function(r){
var limbo = $('<div/>'); //we'll use this to store the new HTML
limbo.html(r); //this will take the string, and insert it as the innerHTML
// from here on, you can use selector functions with the limbo div
// so you can work with them, before you put the contents on the page
var telephone = limbo.find('#telephone');
});
精彩评论