1

我最终会喜欢.cousins(selector=null, uplimiter=-1, upstart=1,excludeself=true),但我不知道这是否适用于插件。如果是,那么我希望将它们作为参数对象传递。

更新 这是工作代码,但部分除外,该excludeself部分旨在丢弃作为上下文子项的任何结果。

(function($) {
$.fn.cousins = function(selector,uplimit,upstart,excludeself) {
  upstart=upstart||1;
  uplimit=uplimit||-1;
  excludeself=arguments.length<4?true:excludeself;
  if (typeof selector==='object') {
    upstart=(typeof selector.upstart!=='undefined')?selector.upstart:upstart;
    uplimit=(typeof selector.uplimit!=='undefined')?selector.uplimit:uplimit;
    excludeself=(typeof selector.excludeself!=='undefined')?selector.excludeself:excludeself;
    selector=selector.selector;
  }
  var cousins=null;
  var upat=0;
  var at=this;
  var that=this;
  if (at.length===0) {
    console.log('r 1');
    return at;
  }
  var upstart_ct=upstart;
  while(at.length>0&&upstart_ct>0) {
    at=at.parent();upat++;
    console.log(at.attr('id'));
    upstart_ct--;
    if (at.length===0) {
      return at;
    }
  }
  console.log('checkiing...');
  while ((cousins===null||cousins.length===0)&&(upat<uplimit||uplimit==-1)) {
    at.each(function() {
      if (cousins!==null) {
        return false;
      }
      var items = $(this).find(selector);
      console.log('found:'+items.length);
      if (0) {// need to excludeself here
        items=items.filter(function(idx){
          var me=$(this);
          var ps=me.parents();
          if (ps.indexOf(that)) {//?? not going to work huh HALP
          }
        });
      }
      if (items.length!=0) {
        if (cousins===null) {
          console.log('assigning to cousins...');
          cousins=items;
          return false;
        }
      }
    });
    if (cousins!==null) {
      console.log('cousins.length'+cousins.length);
    }
    if (cousins) {
      break;
    }
    at=at.parent();upat++;
    console.log('going up...'+upat);
    console.log(at.attr('id'));
    if (at.length===0) {
      cousins=at;
      break;
    }
  }
  return cousins;
  }
})(jQuery);

我以前从未写过 jQuery 插件,所以我需要帮助。

更新,这里是一个html的例子:

.item
  .item-hdr
    .item-hdr-btn <<source
      .item-body-text-area <<an example of excludeself, do not return this in set
  .item-body
    .item-body-textarea <<target

所以我想说$('.item-hdr-btn').cousins('.item-body-textarea')让它在爬上树时返回第一次出现的匹配项。

4

2 回答 2

3

好吧,可以通过以下方式找到表亲:

void function($) {
    $.fn.cousins = function(selector) {
        return this.parent().siblings().children(selector);
        //         ^ parent ^ uncles   ^ cousins
    };
}(jQuery);

修剪中间结果可以相当容易地完成。

默认情况下,节点本身被排除在外,但您可以使用.andSelf()它,然后将其取回。

于 2013-09-21T07:21:42.507 回答
2
(function ($) {
    $.fn.cousins = function(selector) {
        var $parents = this.parents();
        for (var i = 0; i < $parents.length; i++) {
            var $m = $parents.eq(i).siblings(), $c = $(), j = i;
            while (j !== -1) {
                $c = $m = $m.children();
                j--;
            }
            if ($c.length) {
                if (!selector) return $c;
                else return $c.filter(selector);
            }
        }
        return $();
    }
})(jQuery);

http://jsfiddle.net/nJre5/

编辑:我已经更新了该方法,因此它可以接受所需的选项,请注意excludeItself,如果您想根据 jQuery 的版本将原始元素添加到返回的集合中,则不需要像默认情况下那样添加选项您可以在返回的集合上 调用.addBack()或方法。.andSelf()

(function ($) {
    $.fn.cousins = function (o) {
        var options = {
            selector: null,
            uplimiter: 0,
            upstart: 0,
        };
        $.extend(options, o);

        var $p = this.parents(), i = o.upstart, l = $p.length - (o.uplimiter + i);

        for (i; i < l; i++) {
            var $m = $parents.eq(i).siblings(), $c = '', j = i;
            while (j !== -1) {
                $c = $m = $m.children();
                j--;
            }
            if ($c.length) {
                if (o.selector) $c = $c.filter(o.selector);
                // Adding the previous collection to the set
                // so the .addBack() and .andSelf() methods can be called
                $c.prevObject = this;
                return $c;
            }
        }

        var _ = $(); _.prevObject = this; return _;
    }
})(jQuery);

用法:

$('selector').cousins({
    selector: 'element',
    upstart: 2,
    uplimiter: 0
}).addBack() // adding the previous selected elements to the collection
  .foo(); 

方法的名称可能应该是其他名称,例如,elementHunter或其他名称。megaCousinspseudoCousins

于 2013-09-21T06:20:08.937 回答