在jQuery中查找和过滤器之间的区别 [英] Difference between find and filter in jquery

查看:175
本文介绍了在jQuery中查找和过滤器之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从维基页面获取数据。我正在使用 php jquery 的组合来执行此操作。首先,我在 php 中使用 curl 来获取页面内容并回显内容。文件名是 content.php

$ p $ $ url = $ _GET [ 'URL'];
$ url = trim($ url,);
$ url = urldecode($ url);
$ url = str_replace(,%20,$ url);
$ b $ echo< a class ='urlmax'>。$ _ GET ['title']。< / a>;
echo crawl($ url);

然后使用 jQuery 查找匹配元素。

  $。get(content.php,{url:http://en.wikipedia.org/w/ index.php?action = render& title =+ str_replace(,_,data [x]),title:str_replace(,_,data [x])},function(hdata){ 
var imgs = $(hdata).find('a.image img');
var ent = $(hdata).filter('a.urlmax');


ent = $(ent [0]).text();


});

当我使用find而不是filter时,我能够成功获取图像,返回一个空数组。只有过滤器正在工作。为什么会这样?



编辑:我知道find和filter之间的基本区别。这里 a.image img a.urlmax hdata 。那为什么找不到 a.urlmax 。不是 a.urlmax 单独在任何其他类或id上都不起作用

解决方案

.find()
http://api.jquery.com/find/


获取当前匹配元素集合中每个元素的后代,由选择器,jQuery对象或元素过滤


另一方面,过滤器适用于当前匹配的元素。这就是为什么过滤器工作,但找不到(你需要看看当前元素)。

I'm working on fetching data from wiki pages. I'm using a combination of php and jquery to do this. First I am using curl in php to fetch page contents and echoing the content. The filename is content.php:

$url = $_GET['url'];
$url = trim($url," ");
$url = urldecode($url);
$url = str_replace(" ","%20",$url);

echo "<a class='urlmax'>".$_GET['title']."</a>";
echo crawl($url);

Then jQuery is used to find the matched elements.

$.get("content.php",{url:"http://en.wikipedia.org/w/index.php?action=render&title="+str_replace(" ","_",data[x]),title:str_replace(" ","_",data[x])},function(hdata){
                        var imgs = $(hdata).find('a.image img');
                        var ent = $(hdata).filter('a.urlmax');


                        ent = $(ent[0]).text();


});

I was able to successfully get images but for the variable ent when I use find instead of filter, it's returning an empty array. Only filter is working. Why is this?

Edit: I know the basic difference between find and filter. Here both the a.image img and a.urlmax are descendats of the hdata. Then why find does not work on a.urlmax. Not a.urlmax alone it's not working on any other class or id

解决方案

.find() http://api.jquery.com/find/

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

Filter, on the other hand, works on the currently matched elements. That's why filter worked but find did not (you needed to look at the current element).

这篇关于在jQuery中查找和过滤器之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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