jQuery .cousins() method

jQuery .cousins() method

本文关键字:method cousins jQuery      更新时间:2023-09-26

我最终想要.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'),让它在爬上树时返回第一次出现的火柴。

嗯,表兄弟可以找到:

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

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

默认情况下,节点本身被排除在外,但您只需使用.andSelf()即可将其恢复。

(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(); 

可能方法的名称应该是其他东西,比如elementHuntermegaCousinspseudoCousins或其他东西。