码迷,mamicode.com
首页 > 编程语言 > 详细

javascript的一些在IE下不支持的函数小结

时间:2017-11-18 14:20:30      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:自己   eof   ie7   一个   alert   date   string   turn   isarray   

    // ============   isArray  ===============//              
    // isArray  
    function isArray(value){  
        return Object.prototype.toString.call(value) == "[object Array]";  
    }  
    var arr = [1,2,3,4,5];  
    alert(isArray(arr)); // IE8 及以下不支持  

 

  // ============   filter 等  ===============//     
    // 数组的一些方法  every(), filter(), forEach(), map(), some()  
    // IE8 及以下不支持  
    // 解决办法,以filter为例,自己写一个filter  
    if (!Array.prototype.filter) {  
        Array.prototype.filter = function(fun /*, thisp*/){  
            var len = this.length;  
            if (typeof fun != "function"){  
                throw new TypeError();  
            }  
            var res = new Array();  
            var thisp = arguments[1];  
            for (var i = 0; i < len; i++){  
                if (i in this){  
                    var val = this[i]; // in case fun mutates this  
                    if (fun.call(thisp, val, i, this)) {  
                        res.push(val);  
                    }  
                }  
            }  
            return res;  
        };  
    }  
      
    var numbers = [1,2,3,4,5,6];  
    var filterResult = numbers.filter(function(item, inde, array){  
        return (item>2);  
    });  
    alert(filterResult); // 3,4,5,6  

 

    
    // ============   Date.now()  ===============//   
    // Date.now(); IE8及以下不支持,只能自己写一个解决  
    if(!Date.now){  
        Date.now = function(){  
            return new Date().valueOf();  
        }  
    }  
    alert(Date.now());  
      

 

   // ============   stringValue[1]  ===============//  
    // 在IE7 及以下版本显示  undefined    
    var stringValue = "hello world";  
    alert(stringValue[1]);  
                
    
    // ============   trim()  ===============//  
    // 在IE8 及以下版本无效,需要自己写     
    String.prototype.trim = function(){  
        return this.replace(/(^\s*)(\s*$)/g, "");  
    };  
      
    var stringValue2 = "   hello world  ";  
    alert(stringValue2.trim());  

 

javascript的一些在IE下不支持的函数小结

标签:自己   eof   ie7   一个   alert   date   string   turn   isarray   

原文地址:http://www.cnblogs.com/Web-Architecture/p/7856526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!