原生 js 如何实现 jquery 的 closest()方法?
phpcn_u274
phpcn_u274 2017-02-14 13:31:07
0
2
1785

原生 js 如何实现 jquery 的 closest()方法?

情景: 想要查找某个元素的祖先元素, 中间不知相隔多少层? 但是又不想引入jquery整个库。


phpcn_u274
phpcn_u274

모든 응답(2)
数据分析师

네이티브 js에서 jquery의 close() 메소드를 어떻게 구현하나요? -PHP 중국어 Q&A-네이티브 js에서 jquery의 close() 메소드를 어떻게 구현하나요? -PHP 중국어 홈페이지 Q&A

꼭 보고 배워보세요.

阿神

这个问题,你该让jquery回答啊....自己看下源码不就可以了

closest: function( selectors, context ) {
    var cur,
        i = 0,
        l = this.length,
        matched = [],
        pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ?
            jQuery( selectors, context || this.context ) :
            0;

    for ( ; i < l; i++ ) {
        for ( cur = this[ i ]; cur && cur !== context; cur = cur.parentNode ) {

            // Always skip document fragments
            if ( cur.nodeType < 11 && ( pos ?
                pos.index( cur ) > -1 :

                // Don't pass non-elements to Sizzle
                cur.nodeType === 1 &&
                    jQuery.find.matchesSelector( cur, selectors ) ) ) {

                matched.push( cur );
                break;
            }
        }
    }

    return this.pushStack( matched.length > 1 ? jQuery.uniqueSort( matched ) : matched );
},


최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!