项目中常用的javascript/jquery操作

1、判断复选框是否被选中?

$("#cpuWindow").is(':checked');

2、设置复选框被选中:

$("#cpuWindow").prop("checked",true);

3、取小数位数:

(mem_value/1024).toFixed(2);

4、判断某个值是否在元素中:同字符中的indexOf()函数,返回值小于0,则不在

ioTypeArr.indexOf(io[i][2]) < 0

作用:可用于给数组去重;

5、获取当前域:

window.location.host;

6、获取或者设置title:

document.title;

7、map():

map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

map() 方法按照原始数组元素顺序依次处理元素。

注意: map() 不会对空数组进行检测。

注意: map() 不会改变原始数组。

语法:

array.map(function(currentValue,index,arr), thisValue)

currentValue:必须。当前元素的值

index:可选。当期元素的索引值

arr:可选。当期元素属于的数组对象

8、文件大小单位转换:

function unitConversion(size){
    if(size >= 1024 && size < (1024*1024)){
        size = (size/1024).toFixed(2) + "K";
    }else if(size >= (1024*1024) && size < (1024*1024*1024)){
        size = (size/(1024*1024)).toFixed(2) + "M";
    }else if(size >= (1024*1024*1024) && size < (1024*1024*1024*1024)){
        size = (size/(1024*1024*1024)).toFixed(2) + "G";
    }else if(size >= (1024*1024*1024*1024) && size < (1024*1024*1024*1024*1024)){
        size = (size/(1024*1024*1024*1024)).toFixed(2) + "T";
    }else{
        size += "B";
    }
    return size;
}

9、过滤掉html、css、JavaScript:

function filterHtml(html){
    s = html.replace(/<\/?[^>]+>/gi, ''); //定义HTML标签的正则表达式
    s = html.replace(/\\s*|\t|\r|\n/gi, ''); //去除tab、空格、空行
    return s;
}

10、复制到剪贴板:

var urlCode = document.getElementById("share-modal-url-code");
urlCode.select();
document.execCommand("Copy");

11、浏览器检测:

$("#upload-file-button").click(function(){
    var mozilla = /firefox/.test(navigator.userAgent.toLowerCase());
    var webkit = /webkit/.test(navigator.userAgent.toLowerCase());
    var opera = /opera/.test(navigator.userAgent.toLowerCase());
    var msie = /msie/.test(navigator.userAgent.toLowerCase());
    //document.write(navigator.userAgent.toLowerCase());
    
    if(mozilla || webkit || opera){
        $("#upload-file-container").animate({
            bottom: 0
        });
        $("#upload-file-container-tools-up").hide();
        $("#upload-file-container-tools-down").show();
    }else{
        toastr.error("该浏览器不支持断点续传,请使用Chrome、Firefox、Opera浏览器", "错误提示");
    }
});

function IEVersion() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
    var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
    var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    if(isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if(fIEVersion == 7) {
            return 7;
        } else if(fIEVersion == 8) {
            return 8;
        } else if(fIEVersion == 9) {
            return 9;
        } else if(fIEVersion == 10) {
            return 10;
        } else {
            return 6;//IE版本<=7
        }
    } else if(isEdge) {
        return 'edge';//edge
    } else if(isIE11) {
        return 11; //IE11
    }else{
        return -1;//不是ie浏览器
    }
}

持续整理中......

posted @ 2018-12-04 22:05  Samve  阅读(205)  评论(0编辑  收藏  举报