Today, we introduce the small functions of several packages in JS ECMA and some common function cases.
1, find the duplicate function
<script> //Find duplicates inside the array, function Findinarr (N,arr) {for (var i=0; i< arr.length; i++) { if (arr[i ]==n) { return true; } } return false; } </script>
2 Random number function
<script> function rnd (n,m) { return parseint (Math.random () * (m-n) +n) } </script>
3 function of zero complement
<script> function Addzero (n) { return n<10? ' 0 ' +n: ' +n; } </script>
4 summation
<script> function sum () { var res=0; for (Var i=0;i<arguments.length;i++) { res+=arguments[i]; } return res; } Alert (sum (1,2,5,7,9)) </script>
5 Gets the non-inline style function. html
<script> function GetStyle (obj,attr) { if (obj.currentstyle) { return obj.currentstyle[attr]; } else{ return getComputedStyle (Obj,false) [attr]; } } </script>
Here are a few more examples of how to use them
1 Color Ball
<! DOCTYPE html> 2 full upgrade (similar to the effect of a shopping cart)
<! DOCTYPE html>3 Upgraded version of the tab
<! DOCTYPE Html>"en"> <meta charset="UTF-8"> <title></title> <script>window.onload=function () {varObtn=document.getelementbyid ('btn'); varAinput=document.getelementsbytagname ('input'); varinow=0; Obtn.onclick=function () { for(varI=1; i<ainput.length;i++){ if(OBTN.checked==true) {Ainput[i].checked=true; Inow=ainput.length-1; }Else{ainput[i].checked=false; Inow=0; }} document.title=Inow; }; for(varI=1; i<ainput.length;i++) {Ainput[i].onclick=function () {if( This.checked==true) {Inow++; }Else{Inow--; } document.title=Inow; if(inow==ainput.length-1) {obtn.checked=true; }Else{obtn.checked=false; } } } } </script>"checkbox"Name=""Id="btn"/>"checkbox"Name=""Id=""/><input type="checkbox"Name=""Id=""/><input type="checkbox"Name=""Id=""/><input type="checkbox"Name=""Id=""/><input type="checkbox"Name=""Id=""/><input type="checkbox"Name=""Id=""/><input type="checkbox"Name=""Id=""/></body>All right, let's share it with you today, and we'll continue to share other small methods and functions for you tomorrow.
Never too old to learn.
JS_ECMA small functions of several packages in basic syntax-1