jQuery的。将字符串视为HTML文档?

时间:2010-06-13 17:17:09

标签: jquery

我不确定我的标题是否正确。

基本上可以将字符串视为HTML,就好像它在页面上一样吗?所以我可以使用$(“#elem”)和所有其他jQuery函数?

HTML从ajax请求加载到字符串中并存储在字符串中。而不是使用正则表达式来访问所需的数据是否可以使用jQuery函数?

ajaxTextResponse.$("#telephone");

我知道上面的内容不起作用,但是你看到了我的目标。

由于

2 个答案:

答案 0 :(得分:3)

如果您以字符串的形式提供HTML,jQuery将尽力解析它:

success:function(data) {
    $('#el', data).appendTo('body')
}

当#el是某个其他元素的子元素时提取#el的示例。

答案 1 :(得分:0)

是的,您可以将字符串用作HTML,而不是将其加载到页面上,但是,您可能希望先将它存储在div中,然后再使用它,听起来就像。所以,尝试这样的事情:

$.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');

});
相关问题