项目中常用的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

作用:可用于给数组去重,但ie9以下不支持。

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*|	|
|
/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浏览器
    }
}

12、给数组去重:

将数组A中的每一项和一个只有一个元素(该元素等于数组A中的某一项)的数组B中的每一项比较,如果该项不存在,则将该项存入数组B中:

var arr = [1, 2, 3, 4, 5, 6, 7, 8 ,1, 2, 3, 4, 5, 6, 2, 9, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 0, 8 ,9];
var arr2 = [arr[0]];
var num = 0;
for(var i = 0; i<arr.length; i++){
    var num = 0;
    for(var j = 0; j<arr2.length; j++){
        if(arr[i] == arr2[j]){
            num++;
        }
    }
    if(num == 0){
        arr2.push(arr[i]);
    }
}
console.log(arr2);

13、ie8不支持高版本中indexOf()方法(一个数组方法),那自己动手写一个相同功能的函数:

function ArrayIndexOf(arr, value){
  //检测value在arr中出现的位置
  for(var i = 0; i < arr.length; i++){
    if(arr[i] === value){
      return i;
    }
  }
  return -1;
}

14、让JavaScript只在ie下执行:

if (!document.addEventListener) {
    // 解决ie下可能出现文字乱码的问题
    if(location.href.indexOf("#reloaded") == -1){
        location.href = location.href + "#reloaded";
        location.reload();
    }
}

 持续整理中......

原文地址:https://www.cnblogs.com/samve/p/10792993.html