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,"&").replace(/</g,"<").replace(/"/g,""").replace(/>/g,">"); return newStr; } }; console.log('转义后的字符串为:' + '<a href=""> </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);