美文网首页
javascript常见函数2

javascript常见函数2

作者: lucky婧 | 来源:发表于2016-12-24 23:19 被阅读0次

    设置cookie

    function setCookie(name,value,iDate){
        var oDate = new Date();
        oDate.setDate(oDate.getDate()+iDate);
        if(iDate){
            document.cookie = name + '=' +  value +';path=/;expires='+oDate;
        } else {
            document.cookie = name + '=' +  value +';path=/;';
        }
    }
    

    获取cookie

    function getCookie(name){
        var arr = document.cookie.split('; ');
        for (var i=0;i<arr.length;i++){
            var arr2 = arr[i].split('=');
            if(arr2[0] == name){
                return arr2[1];
            }
        }
        return '';
    }
    

    移除指定名称cookie

    function removeCookie(name){
        setCookie(name,1,-1);
    }
    

    json转字符串

    function jsonToStr(json){
        json.t = Math.random();
        var arr = [];
        for (var name in json){
            arr.push(name + '=' + json[name]);
        }
        return arr.join('&');
    }
    

    ajax封装

    function ajax(json){
        var timer = null;
        if(!json.url){
            alert('输入URL');
            return;
        }
        json.data = json.data || {};
        json.time = json.time || 3000;
        json.type = json.type || 'get';
    
        if(window.XMLHttpRequest){
            var oAjax = new XMLHttpRequest();
        } else {
            var oAjax = new ActiveXObject('Microsoft.XMLHTTP');
        }
    
        switch (json.type.toLowerCase()){
            case 'get':
                oAjax.open('GET',json.url+'?'+jsonToStr(json.data),true);
                oAjax.send();
                break;
            case 'post':
                oAjax.open('POST',json.url,true);
                oAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                oAjax.send(jsonToStr(json.data));
                break;
            default:
                alert('输入正确提交方式');
                break;
        }
    
        oAjax.onreadystatechange = function () {
            if (oAjax.readyState == 4){
                if (oAjax.status>=200 && oAjax.status<300 || oAjax.status == 304){
                    json.success && json.success(oAjax.responseText);
                } else {
                    json.error && json.error(oAjax.status);
                }
            }
            clearTimeout(timer);
        };
        timer = setTimeout(function(){
            alert('响应超时!');
            oAjax.onreadystatechange = null;
        },json.time);
    }
    

    jsonp封装

    function jsonp(json){
            if (!json.url) {
                alert('请输入正确的url!');
                return;
            }
            json.data=json.data || {};
            json.cbName=json.cbName || 'cb';
            var fnName='show'+Math.random();
            fnName=fnName.replace('.','');
            window[fnName]=function(jsonD){
                json.fnSucc && json.fnSucc(jsonD);
                //删
                document.head.removeChild(oS);
            };
            json.data[json.cbName]=fnName;
            var arr=[];
            for (var name in json.data) {
                arr.push(name+'='+json.data[name]);
            }
            var oS=document.createElement('script');
            oS.src=json.url+'?'+arr.join('&');
            document.head.appendChild(oS);
        };
    

    正则获取class

    function getClassName(parent,sClass){
        if(document.getElementsByClassName){
            return parent.getElementsByClassName(sClass);
        } else {
            var aELs = parent.getElementsByTagName('*');
            var arr =[];
            for (var i=0;i<aELs.length;i++){
                var reg = new RegExp('\\b'+sClass+'\\b');
                if(reg.test(aELs[i].className)){
                    arr.push(aELs[i]);
                }
            }
            return arr;
        }
    }
    

    正则obj查找class

    function hasClass(obj,sClass){
        var reg = new RegExp('\\b'+sClass+'\\b');
        return reg.test(obj.className);
    }
    

    正则obj添加className

    function addClss(obj,sClass){
        if(obj.className){
            if(!hasClass(obj,sClass)){
                obj.className += ' ' + sClass;
            }
        } else {
            obj.className =  sClass;
        }
    }
    

    正则obj移除className

    function removeClass(obj,sClass){
        var reg = new RegExp('\\b'+sClass+'\\b');
        if(obj.className){
            if(hasClass(obj,sClass)){
                obj.className = obj.className.replace(reg,'').replace(/\s+/g,' ').replace(/^\s+|\s+/g,'');
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:javascript常见函数2

          本文链接:https://www.haomeiwen.com/subject/tysrvttx.html