扩展jquery 1.2以包含索引()

扩展jquery 1.2以包含索引(),jquery,indexing,extend,Jquery,Indexing,Extend,我正在与一些第三方团队合作,无法升级到jquery 1.4+和therfor 1.2。我想知道是否有办法扩展库以包含index()方法。我试着写一个快速函数,但没有成功 function getIndex( $elm ) { var $this = $elm; var $parent = $elm.parent(); var $index = 0; $parent.children().each(function(){ if( $this.leng

我正在与一些第三方团队合作,无法升级到jquery 1.4+和therfor 1.2。我想知道是否有办法扩展库以包含index()方法。我试着写一个快速函数,但没有成功

function getIndex( $elm ) {
    var $this = $elm;
    var $parent = $elm.parent();
    var $index = 0;
    $parent.children().each(function(){
        if( $this.length == $(this).length && $this.length == $this.filter($(this)).length ) {
            return $index;
        }else{
            $index++;   
        }
    });
    return $index;
}
非常感谢您的帮助

问候


菲尔

啊,找到了方法并添加了它

$.fn.index = function(elem){
    // No argument, return index in parent
    if ( !elem ) {
        return ( this[0] && this[0].parentNode ) ? this.prevAll().length : -1;
    }
    // index in selector
    if ( typeof elem === "string" ) {
        return jQuery.inArray( this[0], jQuery( elem ) );
    }
    // Locate the position of the desired element
    return jQuery.inArray(
        // If it receives a jQuery object, the first element is used
        elem.jquery ? elem[0] : elem, this );
}

noConflict()
模式下引入最新版本的jQuery,然后慢慢地将内容转移到该版本……只是一个简单的注释-我在评论中也提到了:-)