js基础2

<html>
<head>
</head>
<body>
<div id="test-div" class="red blue redblue greenred green red"></div>


<script type="text/javascript">
    "use strict";
    var console = window.console;
    var $ = window.$;
    var url = 'https://join.yuewen.com/social.html?key=fds';
    //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>


<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>


<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>


<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>


</body>
</html>

 

posted @ 2019-03-24 16:23  PopeyeSailorMan  阅读(119)  评论(0编辑  收藏  举报