<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • JavaScript下jquery的使用方法

            這篇文章主要介紹“JavaScript下jquery的使用方法”,在日常操作中,相信很多人在JavaScript下jquery的使用方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JavaScript下jquery的使用方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

            懷來網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),懷來網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為懷來數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的懷來做網(wǎng)站的公司定做!

            本文粗燥的實(shí)現(xiàn)jqueryready、each、bind、``$.fn.extend、$.extend

            初始化$

            (function (win) {
              var _$ = function (selector, context) {
                /**
                 * 通常咱們定義一個 函數(shù) var Fun = function(){}
                 * 然后定義一個 Fun.prototype.init = function(){}
                 * 那么咱們調(diào)用init 的時候 得先要實(shí)例化對象 var f = new Fun()
                 * 然后f.init()
                 * 這里就省去了 var $ = new $()
                 */
                return new _$.prototype.Init(selector, context);
              };
              _$.prototype = {
                //初始化$
                Init: function (selector, context) {
                  this.elements = [];
                  /**
                   * 傳入的類型是function 就執(zhí)行ready事件,如果是document 就將document對象插入到this.elements
                   * 主要就是判斷$(document).ready  和 $(function(){}) 這兩種的ready事件的寫法
                   */
                  if (typeof selector === "function") {
                    this.elements.push(document);
                    this.ready(selector);
                  } else {
                    var context = context || document;
                    var isDocument = (ele) =>
                      Object.prototype.toString.call(ele) == "[object HTMLDocument]" ||
                      "[object Document]";
                    if (isDocument(selector)) {
                      this.elements.push(selector);
                    } else {
                      /**
                       * 如果是字符串的話就查詢該節(jié)點(diǎn) $('.class') | $('#id')
                       */
                      if (context.querySelectorAll) {
                        var arr = context.querySelectorAll(selector);
                        for (var i = 0; i < arr.length; i++) {
                          this.elements.push(arr[i]);
                        }
                      }
                    }
                  }
                },
                //實(shí)現(xiàn)each
                each: function (callback) {},
                //實(shí)現(xiàn)ready
                ready: function (callback) {},
                //實(shí)現(xiàn)bind
                bind: function (type, callback) {},
              };
              /**
               * 讓兩個作用域不一樣的對象共享一個方法,讓他們的原型指向一致,即Init.prototype = _$.prototype
               * 那么原型一致之后 就可以共享this.elements 屬性了。
               */
              _$.prototype.Init.prototype = _$.prototype;
              window.$ = _$;
            })(window || global);

            ready

            //實(shí)現(xiàn)ready
            ready: function (callback) {
              var isDocument = (ele) => Object.prototype.toString.call(ele) == '[object HTMLDocument]' || '[object Document]'
              //如果已經(jīng)取得了節(jié)點(diǎn)
              if (isDocument(this.elements[0])) {
                if (document.addEventListener) { //判斷火狐、谷歌
                  /**
                   * DOM樹構(gòu)建完成的時候就會執(zhí)行DOMContentLoaded
                   * 頁面上所有的DOM,樣式表,腳本,圖片,flash都已經(jīng)加載完成了,才會觸發(fā)window.onload
                   * 這也就是$(document).ready() 比 window.onload 執(zhí)行早的原因
                   *
                   * arguments.callee 博客里面有一篇文章 [js-遞歸] 里面專門講到了,這里不再解釋了
                   */
                  document.addEventListener('DOMContentLoaded', function () {
                    document.removeEventListener('DOMContentLoaded', arguments.callee, false)
                    callback()
                  }, false)
                } else if (document.attachEvent) { //判斷IE
                  document.attachEvent('onreadystatechange', function () {
                    if (document.readyState == 'complete') {
                      document.detachEvent('onreadystatechange', arguments.callee);
                      callback()
                    }
                  })
                } else if (document.lastChild == document.body) { //body已經(jīng)加載完了,就直接回調(diào)了
                  callback()
                }
              }
            },

            each

            //實(shí)現(xiàn)each
             each: function (callback) {
                if (this.elements.length > 0) {
                    for (var i = 0; i < this.elements.length; i++) {
                    callback.call(this, this.elements[i], i);
                    }
                }
             },

            bind

            //實(shí)現(xiàn)bind
            bind: function (type, callback) {
              if (document.addEventListener) { //判斷火狐、谷歌
                this.each(function (item, i) {
                    item.addEventListener(type, callback, false)
                })
                } else if (document.attachEvent) { //判斷IE
                this.each(function (item, i) {
                    item.attachEvent('on' + type, callback)
                })
                } else {
                this.each(function (item, i) { //其他瀏覽器 egg: item.onclick = function(){}
                    item['on' + type] = callback
                })
              }
            }

            $.fn.extend/$.extend

            $.fn.extend是為查詢的節(jié)點(diǎn)對象擴(kuò)展方法,是基于$的原型擴(kuò)展的方法

            $.extend是擴(kuò)展常規(guī)方法,是$的靜態(tài)方法

            官方給出解釋:

            jQuery.extend(): Merge the contents of two or more objects together into the first object.(把兩個或者更多的對象合并到第一個當(dāng)中)

            jQuery.fn.extend():Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.(把對象掛載到jQueryprototype屬性,來擴(kuò)展一個新的jQuery實(shí)例方法)

            $.fn.extend方法的初衷是我們擴(kuò)展之后可以用$("").newMetod()這樣訪問,實(shí)際上就是給$原型加一個extend方法。這中間的fn其實(shí)類似于命名空間的作用,沒什么實(shí)際的意義。為的是和$.extend作區(qū)分

            $.fn.extend

            ; (function (win) {
              ...
              _$.prototype.Init.prototype = _$.prototype;
            
               _$.fn = _$.prototype; //把對象掛載到j(luò)Query的prototype屬性
            
              var isObj = (o) => Object.prototype.toString().call(o) === '[object Object]';
              $.fn.extend = function (obj) {
                if (isObj(obj)) {
                  for (var i in obj) {
                    this[i] = obj  //注意這里的this指向是 $.prototype
                  }
                }
              }

            $.extend

            var isObj = (o) => Object.prototype.toString().call(o) === '[object Object]';
            ...
            _$.extend = function (obj) {
                if (isObj(obj)) {
                    for (var i in obj) {
                        this[i] = obj[i]; //注意這里的this指向是 $
                    }
                }
            }

            這倆看上去一模一樣啊,沒啥區(qū)別,注釋里面已經(jīng)說了,this指向不同。咱們來看個例子:

            <!DOCTYPE html>
            <html>
              <head>
                <title>jQuery.extend()與jQuery.fn.extend()區(qū)別</title>
                <meta charset="utf-8" />
                <script type="text/javascript" src="jquery.js"></script>
                <!-- 開始擴(kuò)展 -->
                <script type="text/javascript">
                  (function ($) {
                    $.extend({
                      sayHello: function () {
                        console.log("Hello");
                      },
                    });
                    $.fn.extend({
                      sayHello: function () {
                        console.log("Hello");
                      },
                    });
                  })(jQuery);
                </script>
                <!-- 調(diào)用 -->
                <script type="text/javascript">
                  $(document).ready(function () {
                    //$.extend擴(kuò)展調(diào)用
                    $.sayHello();
            
                    //$.fn.extend擴(kuò)展調(diào)用
                    $("#test").sayHello();
                  });
                </script>
              </head>
              <body>
                <div id="test"></div>
              </body>
            </html>

            這樣以來就看的很明白了。jQuery.extend(object); 為擴(kuò)展jQuery類本身,為自身添加新的方法。$.xxx()

            jQuery.fn.extend(object);jQuery對象添加方法$('#test').xxx()

            $.extend常見用法

            //在jquery全局對象中擴(kuò)展一個net命名空間。
            $.extend({ net: {} });
            
            //方法擴(kuò)展到之前擴(kuò)展的Jquery的net命名空間中去。
            $.extend($.net, {
              sayHello: function () {
                console.log("Hello");
              },
            });
            
            //extend方法還有一個重載原型
            //extend(boolean,dest,src1,src2,src3...),第一個參數(shù)boolean代表是否進(jìn)行深度拷貝
            var a = { protocol: "http", hash: { a: 1, b: 2 } };
            var b = { host: "chuchur.com", hash: { b: 1, c: 2 } };
            
            var result = $.extend(true, {}, a, b);
            console.log(result); //{ protocol: 'http',host: 'chuchur.com', hash: { a: 1, b: 1,c:2 } }
            
            var result = $.extend(false, {}, a, b);
            console.log(result); //{ protocol: 'http',host: 'chuchur.com', hash: { b: 1, c:2 } }

            完整代碼

            (function (win) {
              var _$ = function (selector, context) {
                /**
                 * 通常咱們定義一個 函數(shù) var Fun = function(){}
                 * 然后定義一個 Fun.prototype.init = function(){}
                 * 那么咱們調(diào)用init 的時候 得先要實(shí)例化對象 var f = new Fun()
                 * 然后f.init()
                 * 這里就省去了 var $ = new $()
                 */
                return new _$.prototype.Init(selector, context);
              };
              _$.prototype = {
                //初始化$
                Init: function (selector, context) {
                  this.elements = [];
                  /**
                   * 傳入的類型是function 就執(zhí)行ready事件,如果是document 就將document對象插入到this.elements
                   * 主要就是判斷$(document).ready  和 $(function(){}) 這兩種的ready事件的寫法
                   */
                  if (typeof selector === "function") {
                    this.elements.push(document);
                    this.ready(selector);
                  } else {
                    var context = context || document;
                    var isDocument = (ele) =>
                      Object.prototype.toString.call(ele) == "[object HTMLDocument]" ||
                      "[object Document]";
                    if (isDocument(selector)) {
                      this.elements.push(selector);
                    } else {
                      /**
                       * 如果是字符串的話就查詢該節(jié)點(diǎn) $('.class') | $('#id')
                       */
                      if (context.querySelectorAll) {
                        var arr = context.querySelectorAll(selector);
                        for (var i = 0; i < arr.length; i++) {
                          this.elements.push(arr[i]);
                        }
                      }
                    }
                  }
                },
                //實(shí)現(xiàn)each
                each: function (callback) {
                  if (this.elements.length > 0) {
                    for (var i = 0; i < this.elements.length; i++) {
                      callback.call(this, this.elements[i], i);
                    }
                  }
                },
                //實(shí)現(xiàn)ready
                ready: function (callback) {
                  var isDocument = (ele) =>
                    Object.prototype.toString.call(ele) == "[object HTMLDocument]" ||
                    "[object Document]";
                  //如果已經(jīng)取得了節(jié)點(diǎn)
                  if (isDocument(this.elements[0])) {
                    if (document.addEventListener) {
                      //判斷火狐、谷歌
                      /**
                       * DOM樹構(gòu)建完成的時候就會執(zhí)行DOMContentLoaded
                       * 頁面上所有的DOM,樣式表,腳本,圖片,flash都已經(jīng)加載完成了,才會觸發(fā)window.onload
                       * 這也就是$(document).ready() 比 window.onload 執(zhí)行早的原因
                       *
                       * arguments.callee 博客里面有一篇文章 js-遞歸里面專門講到了,這里不再解釋了
                       */
                      document.addEventListener(
                        "DOMContentLoaded",
                        function () {
                          document.removeEventListener(
                            "DOMContentLoaded",
                            arguments.callee,
                            false
                          );
                          callback();
                        },
                        false
                      );
                    } else if (document.attachEvent) {
                      //判斷IE
                      document.attachEvent("onreadystatechange", function () {
                        if (document.readyState == "complete") {
                          document.detachEvent("onreadystatechange", arguments.callee);
                          callback();
                        }
                      });
                    } else if (document.lastChild == document.body) {
                      //body已經(jīng)加載完了,就直接回調(diào)了
                      callback();
                    }
                  }
                },
                //實(shí)現(xiàn)bind
                bind: function (type, callback) {
                  if (document.addEventListener) {
                    //判斷火狐、谷歌
                    this.each(function (item, i) {
                      item.addEventListener(type, callback, false);
                    });
                  } else if (document.attachEvent) {
                    //判斷IE
                    this.each(function (item, i) {
                      item.attachEvent("on" + type, callback);
                    });
                  } else {
                    this.each(function (item, i) {
                      //其他瀏覽器 egg: item.onclick = function(){}
                      item["on" + type] = callback;
                    });
                  }
                },
              };
              /**
               * 讓兩個作用于不一樣的對象共享一個方法,讓他們的原型指向一直,即Init.prototype = _$.prototype
               * 那么指向之后 就可以共享this.elements 屬性了。
               */
              _$.prototype.Init.prototype = _$.prototype;
            
              var isObj = (o) => Object.prototype.toString().call(o) === "[object Object]";
              $.fn.extend = function (obj) {
                if (isObj(obj)) {
                  for (var i in obj) {
                    this[i] = obj; //注意這里的this指向是 $.prototype
                  }
                }
                //....這里是簡寫
              };
            
              _$.extend = function (obj) {
                if (isObj(obj)) {
                  for (var i in obj) {
                    this[i] = obj[i]; //注意這里的this指向是 $
                  }
                }
                //....這里是簡寫
              };
            
              window.$ = _$;
            })(window || global);

            到此,關(guān)于“JavaScript下jquery的使用方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

            網(wǎng)頁標(biāo)題:JavaScript下jquery的使用方法
            網(wǎng)頁鏈接:http://www.jbt999.com/article34/gjsdpe.html

            成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、自適應(yīng)網(wǎng)站、網(wǎng)站排名、網(wǎng)站營銷面包屑導(dǎo)航、云服務(wù)器

            廣告

            聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

            綿陽服務(wù)器托管

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 欧美成人免费网 | 国产三级片网站 | 久爱avcn | 女人高潮免费视频 | 中文字幕日产一区 |