JS_ECMA small functions of several packages in basic syntax-1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.