zoukankan      html  css  js  c++  java
  • 项目中常用的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*|	|
    |
    /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浏览器
        }
    }

    持续整理中......

  • 相关阅读:
    HDU 1069 Monkey and Banana
    HDU 1029 Ignatius and the Princess IV
    HDU 1024 Max Sum Plus Plus
    Gym100923H Por Costel and the Match
    Codeforces 682C Alyona and the Tree
    Codeforces 449B Jzzhu and Cities
    Codeforces (ccpc-wannafly camp day2) L. Por Costel and the Semipalindromes
    Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum
    Codeforces 1167c(ccpc wannafly camp day1) News Distribution 并查集模板
    快乐数问题
  • 原文地址:https://www.cnblogs.com/samve/p/10067667.html
Copyright © 2011-2022 走看看