Actually, maybe this is better:

$(function() {
    var selection = $(".container");
    var target = $("a:first");
    while ((target = target.parent()).length)
        if (selection.filter(function(){return this == target.get(0)}).length)
            break;
    $(target).css('background-color', 'red');
});


That avoids fetching parent() twice.

-- dz



On Fri, Feb 6, 2009 at 7:44 PM, David Zhou <da...@nodnod.net> wrote:
> Hmm, what about:
>
> $(function() {
>    var selection = $(".container");
>    var target = $("a:first");
>    while (target.parent().length && (target = target.parent()))
>        if (selection.filter(function(){return this == target.get(0)}).length)
>            break;
>    $(target).css('background-color', 'red');
> });
>
> That avoids the initial parents() loop, and returns immediately
> without needing to filter targetParents for every container.
>
>
>
> On Fri, Feb 6, 2009 at 6:02 PM, Jörn Zaefferer
> <joern.zaeffe...@googlemail.com> wrote:
>>
>> Thanks David, that got me thinking in the right direction, ending up
>> with this: http://jsbin.com/oyiyu/edit
>>
>> $(function() {
>>  var selection = $(".container");
>>  var target = $("a:first");
>>  var targetParents = target.parents();
>>  var filtered = selection.filter(function() {
>>    var container = this;
>>    return targetParents.filter(function() { return this == container;
>> }).length;
>>  });
>>  filtered.css("background-color", "red");
>> });
>>
>> That selects the target's parents once, and filters them once for each
>> container, which is quite acceptable. There probably still is some
>> better way...
>>
>> Jörn
>>
>> On Fri, Feb 6, 2009 at 6:09 PM, David Zhou <da...@nodnod.net> wrote:
>>>
>>> What about something like (and this is very rough, but hopefully
>>> conveys the general idea):
>>>
>>> jQuery.fn.hasDescendant = function(descendant) {
>>>    jQuery.data(this.get(0), "has_d", "found");
>>>    descendant = jQuery(descendant);
>>>    while (descendant.parent().length) {
>>>        if (jQuery.data(descendant.parent().get(0), "has_d") == "found") {
>>>            jQuery.removeData(descendant.parent().get(0), "has_d");
>>>            return true;
>>>        } else
>>>            descendant = descendant.parent();
>>>    }
>>>    jQuery.removeData(this.get(0), "has_d");
>>>    return false;
>>> }
>>>
>>> -- dz
>>>
>>>
>>>
>>> On Fri, Feb 6, 2009 at 10:43 AM, Jörn Zaefferer
>>> <joern.zaeffe...@googlemail.com> wrote:
>>>>
>>>> Nope, that isn't it. My example is simplified, and the target comes an
>>>> event triggered elsewhere. So target is just a reference to a DOM
>>>> element, and can't be replaced with some selector.
>>>>
>>>> Jörn
>>>>
>>>> On Fri, Feb 6, 2009 at 3:46 PM, David Zhou <da...@nodnod.net> wrote:
>>>>>
>>>>> Isn't that basically:
>>>>>
>>>>> if ($('.container a:first').length)
>>>>>    $('.container').css('background-color', 'red');
>>>>>
>>>>> Unless I misunderstand what you're asking for?
>>>>>
>>>>> -- dz
>>>>>
>>>>>
>>>>>
>>>>> On Fri, Feb 6, 2009 at 6:58 AM, Jörn Zaefferer
>>>>> <joern.zaeffe...@googlemail.com> wrote:
>>>>>>
>>>>>> I'm trying to solve the follow selector problem: Assuming two
>>>>>> container elements that both have arbitrary descendents. Given a
>>>>>> jQuery object containing these containers and one descendent of any of
>>>>>> these, whats the most effecient way to find the correct parent
>>>>>> element? Neither parents() nor closest() helps, as there is no
>>>>>> sufficient simple selector available.
>>>>>> In other words, I want to filter a selection by a descendent.
>>>>>>
>>>>>> My naiive implementation goes through all descendents, which isn't
>>>>>> really acceptable: http://jsbin.com/uhama/edit
>>>>>>
>>>>>> Ideas?
>>>>>>
>>>>>> Jörn
>>>>>>
>>>>>> >
>>>>>>
>>>>>
>>>>> >
>>>>>
>>>>
>>>> >
>>>>
>>>
>>> >
>>>
>>
>> >>
>>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to