jQuery 2.1.4版本的源碼分析

來源:互聯網
上載者:User

標籤:elements   doc   and   name   div   []   失效   asp   dir   

jQuery 2.1.4版本的源碼分析

jquery中擷取元素的源碼分析

jQuery.each({
// 擷取當前元素的父級元素 parent:
function(elem) { var parent = elem.parentNode;
//nodeType為11的節點類型是DocumentFragment
return parent && parent.nodeType !== 11 ? parent : null; },
//擷取所有的 父節點 這涉及到 dir 方法 parents:
function(elem) { return jQuery.dir(elem, "parentNode"); }, parentsUntil: function(elem, i, until) { return jQuery.dir(elem, "parentNode", until); }, next: function(elem) { return sibling(elem, "nextSibling"); }, prev: function(elem) { return sibling(elem, "previousSibling"); }, nextAll: function(elem) { return jQuery.dir(elem, "nextSibling"); }, prevAll: function(elem) { return jQuery.dir(elem, "previousSibling"); }, nextUntil: function(elem, i, until) { return jQuery.dir(elem, "nextSibling", until); }, prevUntil: function(elem, i, until) { return jQuery.dir(elem, "previousSibling", until); },
     //返回所有的兄弟節點 siblings:
function(elem) { return jQuery.sibling((elem.parentNode || {}).firstChild, elem); }, children: function(elem) { return jQuery.sibling(elem.firstChild); },
    //返回架構裡面的所有內容 或者 元素的所有子節點 contents:
function(elem) { return elem.contentDocument || jQuery.merge([], elem.childNodes); } }, function(name, fn) { jQuery.fn[name] = function(until, selector) { var matched = jQuery.map(this, fn, until); if(name.slice(-5) !== "Until") { selector = until; } if(selector && typeof selector === "string") { matched = jQuery.filter(selector, matched); } if(this.length > 1) { // Remove duplicates if(!guaranteedUnique[name]) { jQuery.unique(matched); } // Reverse order for parents* and prev-derivatives if(rparentsprev.test(name)) { matched.reverse(); } } return this.pushStack(matched); }; });

 

jQuery.extend({
// 尋找父級 直到 找到until結束 根據parents 方法可知 dir = parentNode dir:
function(elem, dir, until) { var matched = [], truncate = until !== undefined;        //進入迴圈的條件 不是根節點 且 且吧父節點複製給elem while((elem = elem[dir]) && elem.nodeType !== 9) { if(elem.nodeType === 1) {
            //跳出迴圈
if(truncate && jQuery(elem).is(until)) { break; } matched.push(elem); } } return matched; },         //通過上面的源碼next方法可知 elem = nextSiblilng sibling: function(n, elem) { var matched = []; for(; n; n = n.nextSibling) { if(n.nodeType === 1 && n !== elem) { matched.push(n); } } return matched; } });

這裡說一下 nextSibling 與 nextElementSibling的區別

nextSibling :返回元素之後的兄弟節點 (包含 注釋 文本)

nextElementSibling:返回元素之後的兄弟節點 不包括注釋 跟文本 且他的屬性只為讀

jQuery.noConflict源碼分析

var    // 將JQuery掛載到Windows對象下        _jQuery = window.jQuery,        // 將$掛載到Windows對象下        _$ = window.$;   // 當調用 Jquery.noConfict  deep ==false  那麼  $ 別名失效  如果   deep==true 那麼 JQuery 的別名失效    jQuery.noConflict = function(deep) {        if(window.$ === jQuery) {            window.$ = _$;        }        if(deep && window.jQuery === jQuery) {            window.jQuery = _jQuery;        }        return jQuery;    };    // Expose jQuery and $ identifiers, even in AMD    // (#7102#comment:10, https://github.com/jquery/jquery/pull/557)    // and CommonJS for browser emulators (#13566)    if(typeof noGlobal === strundefined) {        window.jQuery = window.$ = jQuery;

2016-11-22   21:14:38

jQuery 2.1.4版本的源碼分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.