Common JS snippet

1. The hidden part of the number, such as phone number, ID number

      
      
1
2
3
      
      
function (str,start,length,mask_char){
return str.replace(str.substr(start,length), Array(length+ 1).join(mask_char|| "*"))
}

2. Obtain a random number within the specified range

      
      
1
2
3
      
      
function Randum ( mention maxnum ) {
return Math .floor (My + Math .random () * (maxnum memory));
}

randNum (0,10) is obtained random number between 0 and 9

3. Select All, Select None

      
      
1
2
3
4
5
6
      
      
<div>
<input type="checkbox" id="checkAll"/>全选
< The INPUT of the type = "the CheckBox" /> eating
< The INPUT of the type = "the CheckBox" /> Sleeping
< The INPUT of the type = "the CheckBox" /> play Peas
</div>
      
      
1
2
3
4
5
6
7
8
9
      
      
was checkAll = document .getElementById ( 'checkAll' );
var checkBoxs = document.getElementsByTagName( 'input');
大专栏   常用JS代码片段s="line"> var select = false;
checkAll.addEventListener( 'click', function(){
select = !select;
for( let i in checkBoxs){
checkBoxs[i].checked = select;
}
}, false)

4.js原生判断元素是否隐藏

当容器元素的style.display 被设置为 “none”时(IE和Opera除外),offsetParent属性返回 null。

      
      
1
2
3
      
      
var isHidden = function (element) {
return (element.offsetParent === null);
};

5.jquery中判断对象是否为空对象

空对象{}肯定是没有属性的,所以通过遍历他的属性来判断它是否是空对象

      
      
1
2
3
4
5
6
      
      
function isEmptyObject(e) {
var t;
for (t in e)
return ! 1;
return ! 0
}

5.数组去重

判断当前元素的位置是否是该元素第一次出现的位置,如果不是说明元素重复。indexOf是查找元素第一次出现位置的索引值

      
      
1
2
3
4
5
      
      
function uniq(array){
return Array.prototype.filter.call(array, function(item,idx){
return array.indexOf(item) == idx;
})
}

Guess you like

Origin www.cnblogs.com/lijianming180/p/12046761.html