用jQuery解析部分HTML字符串 [英] parse partial html string with jquery

查看:149
本文介绍了用jQuery解析部分HTML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用$.ajax()从服务器中获取一些HTML页面. 重新运行包含完整的HTML结果.但是我只对文档中的一个非常特定的div感兴趣.

I use $.ajax() to get some HTML pages from my server. The retrun contains the full HTML result. But i am only interested in a very specific div inside this document.

唯一给定的事情是我的ajax成功函数返回一个JSON对象.我已经制作了一个PHP代理文件,用于其他用途,它向我返回了一个带标头的JSON对象,该信息包括我正在加载的文件及其内容.所以基本上我有一个包含页面整个HTML的字符串.

The only given thing is that my ajax success function returns a JSON object. I have made a PHP proxy file i use for other stuff that returns me a JSON object with the headers, some information to the file I'm loading and its contents. So basically i have a string containing the whole HTML of the page.

实际上我是这样的:$($(data.content)[21]) 但这是一个可怕的jquery示例(因为我两次使用了$选择器,并且HTML可以通过更改,而我感兴趣的div可以更改jquery数组中的位置).我只想获取div <div id="items">...</div>及其内容,然后再使用jquery选择它.

Actually i make it like that: $($(data.content)[21]) but this is awful example of jquery (Because i use the $ selector twice, and the HTML could by changing and the div I'm interested in could be changing position in the jquery array). I would like to get only the div <div id="items">...</div> and its contents and only then select it with jquery.

此案例的最佳做法是什么?您的方法是什么样的?

What is the best practice for this case? What would your approach look like?

PS:如果我的示例不清楚,您可以在此处查看示例: http://meodai.ch/content_slider/

PS: in case that my example is not clear, your can see an example here: http://meodai.ch/content_slider/

推荐答案

success: function(result) {
    var div = $(result).filter('#items');
}

这篇关于用jQuery解析部分HTML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆