• js基础1


    1.取到URL后面的参数:

    <script type="text/javascript">
        "use strict";
        var console = window.console;
        var $ = window.$;
        var url = 'location.href';
        //split() 方法用于把一个字符串分割成字符串数组,并且具有数组的属性。此时key=["a=1","b=2"];
        var key = url.split('?')[1].split('&'); 
        // 建立一个空object对象来储存新的object对象
        var obj = {};
        // 循环遍历数组的每一项,并且将a=1格式转换成a:1
        for(var i=0;i<key.length;i++) {
            var arr = key[i].split('=');
            var name = arr[0];
            obj[name] = arr[1];
        }
        console.log(obj);
    </script>

    2.移除某个dom的某个类名

    <script type="text/javascript">
    function removeClass(target,remove) {
        //  判断这个class前后是否有空格来判断他是不是一个独立的class 而不是包含在另一字符串里面的class 
        var re = new RegExp('\s+'+ remove +'\s+', 'g');
        //  因此要在将这串classname的前后都加上空格
            target.className = " " + target.className+ " "; 
        // 判断改字符串是否符合正则
        if (re.test(target.className)){
            // 若是则将这个这个字符和他的前后空格移除
            target.className = target.className.replace(re ,"");
            console.log('移除red这个class后的classname为:');
            console.log(target.className);
        }
        else{
            console.log("has no class "+ remove);
        }
    }
    var target = document.getElementById('test-div');
    removeClass(target,"red");
    </script>

    3.对字符进行转义

    <script type="text/javascript">
    String.prototype.encodeHTML = function() {
        var newStr = ""; 
        if (this.length == 0) {
            return "";
        } else {
            newStr = this.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/"/g,"&quot;").replace(/>/g,"&gt;");
            return newStr; 
        }
    };
    console.log('转义后的字符串为:' + '<a href="">&nbsp;</a>'.encodeHTML());
    </script>

    4.求百分比并补0

    <script type="text/javascript">
        var divide = function(loaded,total) {
            var result = (loaded / total)*10000 / 100;
            result = result.toFixed(2) + '%';
            console.log(result);
        }
        divide(33,100);
        divide(3,100);
        divide(0.3,100);
    </script>

    5.输出2017年08月17日的格式:

    <script type="text/javascript">
        "use strict";
        var console = window.console;
    
    
        var date = new Date(),
            year = date.getFullYear()+'年',
            month = date.getMonth()+'月',
            day = date.getDay()+'日';
    
        function time () {
            var timeMsg = '';
            for(i=0; i<arguments.length; i++) {
                if(arguments[i].toString().length < 3) {
                    arguments[i] = '0' + arguments[i];
                } else {
                    arguments[i] = arguments[i];
                }
                timeMsg += arguments[i];
            }
            return timeMsg;
        }
        console.log(time(year,month,day));
    </script>

    6.循环data里面的内容并且输出

    <script>    
    var data = [{
                "id": 101,
                "value": '第一条数据'
            },
            {
                "id": 101,
                "value": '第一条数据'
            },
            {
                "id": 101,
                "value": '第一条数据'
            }
        ];
        var complied = function(arr) {
            var html = '';
                html = html + "<ul>
    ";
            data.forEach(function(obj){
                html += '<li id="list' + obj.id + '">' + obj.value + '</li>
    ';
            });
            html = html + "</ul>" ;
            return html;
        };
        console.log(complied(data));
    </script>

     7.将字符转换成国际标准法

    // 方法1
        var valueToNumber = function(value) {
            var sValue = value.toString();
            sValue = sValue.split('');
            sValue.reverse();
            for (var i=0; i< sValue.length; i++) {
                if(i%4 == 3) {
                    sValue.splice(i,0,',');            
                }
            }
            value = sValue.reverse().join('');
            console.log(value);
            return value;
        };
        valueToNumber(1000);
        valueToNumber(123456789);
    
    
    // 方法2
        var valueToNumber2 = function(value) {
            console.log(value.toLocaleString('en-US'));
            return value.toLocaleString('en-US');
        };
        valueToNumber2(1000);
        valueToNumber2(1234567);

    8.数组基础知识:

        var a = ['a', 'b', 'c'];
        var b = ['x', 'y', 'z'];
        var c = a.concat(b, false);
    
        console.log('1.' + a);  // ['a','b','c'];
        console.log('1.' + c);  // ['a','b','c','x','y','z',false];
    
        var a = ['a','b','c'];
        var b = ['x','y','z'];
        var c = a.push(b, true);
    
        console.log('2.' + a);  // ['a','b','c',['x','y','z'],true];
        console.log('2.' + c);     // 5 返回数组的长度
    
        //扩展:
        //若要把两个两个数组合并,使用apply()方法;
        // var d = a.push.apply(a,b);
        // var e = a.push(b);
        // console.log(a); 
        // console.log(e);
    
    
        var n = ['123', '23', '32', 'a', 'aa'];
        n.sort();
        console.log('3.' + n);
    
        var n = ['123', '23', '32', 'a', 'aa'];
        n.sort(function(a,b) {
            return a-b;  //
        });
        console.log('4.' + n);  //["23", "32", "123", "a", "aa"]
    
        // 扩展:
        var scores = [1, 10, 21, 2]; 
        scores.sort(); 
        //console.log(scores);
        // [1, 10, 2, 21]
        // 注意10在2之前,
        // 因为在 Unicode 指针顺序中"10"在"2"之前
        var things = ['word', 'Word', '1 Word', '1 Word3', '2 Words'];
        things.sort(); 
        //console.log(things);
        // ['1 Word', '2 Words', 'Word', 'word']
        // 在Unicode中, 数字在大写字母之前,
        // 大写字母在小写字母之前.字母短的在字母串长的前面
        
    
        var y = [5,8,3,10];
        var f = y.some(function(v){
            return v>7;
        });
        console.log('5.' + f); //ture
    
        var x = [5,8,3,10];
        var h = x.every(function(v){
            return v>7;
        });
        console.log('6.' + h);
  • 相关阅读:
    ocx手动添加自定义消息
    ocx手动添加方法
    ocx手动添加事件
    c判断文件是否存在
    unity hub 更新之后,出现了好多问题。
    asp.net core系列 77 webapi响应压缩
    python:生成半年内的巡检日报execl
    python:selenium爬取boss网站被关小黑屋
    k8s 批量安装脚本
    sysctl: cannot stat /proc/sys/net/bridge/bridgenfcallip6tables: No such file or directory
  • 原文地址:https://www.cnblogs.com/popeyesailorman/p/7381656.html
Copyright © 2020-2023  润新知