亚洲免费乱码视频,日韩 欧美 国产 动漫 一区,97在线观看免费视频播国产,中文字幕亚洲图片

      1. <legend id="ppnor"></legend>

      2. 
        
        <sup id="ppnor"><input id="ppnor"></input></sup>
        <s id="ppnor"></s>

        淺析JavaScript中瀏覽器的兼容問(wèn)題

        字號(hào):


            下面小編就為大家?guī)?lái)一篇淺析JavaScript中瀏覽器的兼容問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考
            瀏覽器兼容性問(wèn)題是在實(shí)際開(kāi)發(fā)中容易忽略而又最重要的一部分。我們?cè)谥v老版本瀏覽器兼容問(wèn)題之前,首先要了解什么是能力檢測(cè),它是來(lái)檢測(cè)瀏覽器有沒(méi)有這種能力,即判斷當(dāng)前瀏覽器是否支持要調(diào)用的屬性或者方法。下面做了一些簡(jiǎn)短的介紹。
            1、innerText 和 innerContent
            1)innerText 和 innerContent 的作用相同
            2)innerText IE8之前的瀏覽器支持
            3)innerContent 老版本的Firefox支持
            4)新版本的瀏覽器兩種方式都支持
            1 // 老版本瀏覽器兼容 innerText 和 innerContent
            2 if (element.textContent) {
            3    return element.textContent ;
            4  } else {
            5    return element.innerText;
            6  }
            2、獲取兄弟節(jié)點(diǎn)/元素的兼容性問(wèn)題
             1)兄弟節(jié)點(diǎn),所有瀏覽器都支持
            ①nextSibling 下一個(gè)兄弟節(jié)點(diǎn),可能是非元素節(jié)點(diǎn);會(huì)獲取到文本節(jié)點(diǎn)
            ②previousSibling  上一個(gè)兄弟節(jié)點(diǎn),可能是非元素節(jié)點(diǎn);會(huì)獲取到文本節(jié)點(diǎn)
             2)兄弟元素,IE8以前不支持
            ①previousElementSibling 獲取上一個(gè)緊鄰的兄弟元素,會(huì)忽略空白  
            ②nextElementSibling  獲取下一個(gè)緊鄰的兄弟元素,會(huì)忽略空白
            //兼容瀏覽器
            // 獲取下一個(gè)緊鄰的兄弟元素
            function getNextElement(element) {
              // 能力檢測(cè)
             if(element.nextElementSibling) {
               return element.nextElementSibling;
              } else {
                 var node = element.nextSibling;
                 while(node && node.nodeType !== 1) {
                     node = node.nextibling;
                 }
                 return node;
              }
             }
            ----------------------------------------------------------------------
            /**
            * 返回上一個(gè)元素
            * @param element
            * @returns {*}
            */
            function getPreviousElement(element) {
              if(element.previousElementSibling) {
                return element.previousElementSibling;
              }else {
                var el = element.previousSibling;
                while(el && el.nodeType !== 1) {
                  el = el.previousSibling;
                  }
                return el;
              }
            }
            --------------------------------------------------------------------
            /**
            * 返回第一個(gè)元素firstElementChild的瀏覽器兼容
            * @param parent
            * @returns {*}
            */
            function getFirstElement(parent) {
              if(parent.firstElementChild) {
                return parent.firstElementChild;
              }else {
                var el = parent.firstChild;
                while(el && el.nodeType !== 1) {
                  el = el.nextSibling;
                  }
                return el;
              }
            }
            ---------------------------------------------------------------------
            /**
            * 返回最后一個(gè)元素
            * @param parent
            * @returns {*}
            */
            function getLastElement(parent) {
              if(parent.lastElementChild) {
                return parent.lastElementChild;
              }else {
                var el = parent.lastChild;
                while(el && el.nodeType !== 1) {
                  el = el.previousSibling;
                  }
                return el;
              }
            }
            -------------------------------------------------------------------
            /**
            *獲取當(dāng)前元素的所有兄弟元素
            * @param element
            * @returns {Array}
            */
            function sibling(element) {
              if(!element) return ;
               
              var elements = [ ];
              var el = element.previousSibling;
              while(el) {
                if(el.nodeType === 1) {
                  elements.push(el);
                }
                el = el.previousSibling;
              }
               el = element.previousSibling;
               while(el ) {
                if(el.nodeType === 1) {
                  elements.push(el);
                }
                el = el.nextSibling;
              }
                return elements;
            }
            3、array.filter();   
             // 使用指定的函數(shù)測(cè)試所有元素,并創(chuàng)建一個(gè)包含所有通過(guò)測(cè)試的元素的新數(shù)組
            // 兼容舊環(huán)境
            if (!Array.prototype.filter)
            {
             Array.prototype.filter = function(fun /*, thisArg */)
             {
              "use strict";
              if (this === void 0 || this === null)
               throw new TypeError();
              var t = Object(this);
              var len = t.length >>> 0;
              if (typeof fun !== "function")
               throw new TypeError();
              var res = [];
              var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
              for (var i = 0; i < len; i++)
              {
               if (i in t)
               {
                var val = t[i];
                // NOTE: Technically this should Object.defineProperty at
                //    the next index, as push can be affected by
                //    properties on Object.prototype and Array.prototype.
                //    But that method's new, and collisions should be
                //    rare, so use the more-compatible alternative.
                if (fun.call(thisArg, val, i, t))
                 res.push(val);
               }
              }
              return res;
             };
            }
            4、array.forEach();
            // 遍歷數(shù)組
            //兼容舊環(huán)境
            // Production steps of ECMA-262, Edition 5, 15.4.4.18
            // Reference: http://es5.github.io/#x15.4.4.18
            if (!Array.prototype.forEach) {
             Array.prototype.forEach = function(callback, thisArg) {
              var T, k;
              if (this == null) {
               throw new TypeError(' this is null or not defined');
              }
              // 1. Let O be the result of calling toObject() passing the
              // |this| value as the argument.
              var O = Object(this);
              // 2. Let lenValue be the result of calling the Get() internal
              // method of O with the argument "length".
              // 3. Let len be toUint32(lenValue).
              var len = O.length >>> 0;
              // 4. If isCallable(callback) is false, throw a TypeError
              exception. // See: http://es5.github.com/#x9.11
              if (typeof callback !== "function") {
               throw new TypeError(callback + ' is not a function');
              }
              // 5. If thisArg was supplied, let T be thisArg; else let
              // T be undefined.
              if (arguments.length > 1) {
               T = thisArg;
              }
              // 6. Let k be 0
              k = 0;
              // 7. Repeat, while k < len
              while (k < len) {
               var kValue;
               // a. Let Pk be ToString(k).
               //  This is implicit for LHS operands of the in operator
               // b. Let kPresent be the result of calling the HasProperty
               //  internal method of O with argument Pk.
               //  This step can be combined with c
               // c. If kPresent is true, then
               if (k in O) {
                // i. Let kValue be the result of calling the Get internal
                // method of O with argument Pk.
                kValue = O[k];
                // ii. Call the Call internal method of callback with T as
                // the this value and argument list containing kValue, k, and O.
                callback.call(T, kValue, k, O);
               }
               // d. Increase k by 1.
               k++;
              }
              // 8. return undefined
             };
            }
            5、注冊(cè)事件
            .addEventListener = function (type,listener,useCapture ) { }; 
            //第一個(gè)參數(shù) 事件名稱 
            //第二個(gè)參數(shù) 事件處理函數(shù)(監(jiān)聽(tīng)者) 
            //第三個(gè)參數(shù) true捕獲 false冒泡 
            //IE9以后才支持 
            // 兼容舊環(huán)境
            var EventTools = {
                addEventListener: function (element, eventName, listener) {
                  //能力檢測(cè)
                  if(element.addEventListener) {
                    element.addEventListener(eventName, listener,false);
                  }else if(element.attachEvent) {
                    element.attachEvent("on" + eventName, listener);
                  }else{
                    element["on" + eventName] = listener;
                  }
                },
            //  想要移除事件,不能使用匿名函數(shù)
                removeEventListener: function (element, eventName, listener) {
                  if(element.removeEventListener) {
                    element.removeEventListener(eventName,listener,false);
                  }else if(element.detachEvent) { //IE8以前注冊(cè).attachEvent和移除事件.detachEvent
                    element.detachEvent("on"+eventName,listener);
                  }else{
                    element["on" + eventName] = null;
                  }
                }
              };
            6、事件對(duì)象
             1)事件參數(shù)e,就是事件對(duì)象,標(biāo)準(zhǔn)的獲取方式
            btn.onclick = function(e) { }
             2)e.eventPhase 事件階段,IE8以前不支持
             3)e.target 始終是觸發(fā)事件的對(duì)象(點(diǎn)擊的按鈕)
               i)IE8以前 srcElement
               ii)瀏覽器兼容
            var target = e.target || window.event.srcElement;
            // 獲取事件對(duì)象 兼容瀏覽器
             getEvent: function(e) {
              return e || window.event; // e事件對(duì)象 標(biāo)準(zhǔn)的獲取方式; window.event IE8以前獲取事件對(duì)象的方式
             }
            // 兼容target
             getTarget: function(e) {
              return e.target || e.srcElement;
             }
            7、獲取鼠標(biāo)在頁(yè)面上的位置
            ①在可視區(qū)域中的位置:  e.clientX   e.clientY
            ②在文檔中的位置: 
              i) e.pageX      e.pageY
              ii)瀏覽器兼容
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
             var pageY = e.clientY + scrollTop;
            8、獲取頁(yè)面滾動(dòng)的距離
            // 兼容瀏覽器
            var scrollTop = document.documentElement.scrollTop || document.body.scrolltop;
            9、取消文本的選擇
            // 兼容瀏覽器
             window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
            【總結(jié)】這里只是做了一部分的小結(jié),實(shí)際開(kāi)發(fā)中也還會(huì)遇到各種瀏覽器兼容的問(wèn)題。不同瀏覽器在PC端和手機(jī)端也會(huì)遇到不同適配問(wèn)題,這些就有待童鞋們一起去發(fā)掘總結(jié)啦~~希望能幫到大家,不足的地方請(qǐng)多指教啦~~~
            以上這篇淺析JavaScript中瀏覽器的兼容問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考