2016-05-29 for loops several applications

Source: Internet
Author: User

1. Ladder

:

    <style>        div{width:30px; height:30px; border:1px solid royalblue; background:red; position:absolute;top:0px ; Left:0px;color: #fff}    </style>    <script>        window.onload=function () {            var str = ';            for (Var i=0;i<11;i++) {                str+= "<div>" +i+ "</div>";            }            Document.body.innerhtml=str;            var divs=document.getelementsbytagname ("div");            Len=divs.length;            for (Var i=0;i<len;i++) {                divs[i].style.left+=i*50+10+ "px";//div around 10px pixels                divs[i].style.top+=i*50 +10+ "px";//each div is spaced 10px pixels above and below            }    </script>

  

2.V character is an algorithm, when the number in half +1, the number increases, more than half of the time the number increases the value is less 6->130 7->100 8->70 ...

<script>window.onload=function(){            varstr = ";  for(vari=0;i<11;i++) {str+ = "<div>" +i+ "</div>"; } Document.body.innerHTML=str; varDivs=document.getelementsbytagname ("div"); Len=divs.length; varDown=len%2==0?len/2:math.ceil (LEN/2); varcount=2;  for(vari=0;i<len;i++){                if(i<Down ) {Divs[i].style.left+=i*30+10+ "px";//div distance to the leftmost 10px spacing divs[i].style.top+=i*30+10+ "px";//div distance from the top of the 10px spacingalert (divs[i].style.top); }Else if(I>=down &&i<Len) {Divs[i].style.left+=i*30+10+ "px"; Divs[i].style.top+ = (i-count) *30+10+ "px";//The essence is an algorithm number increases the value but becomes less 6->130 7->100 8->70 ...count+=2; }            }        }    </script>

3. Nesting for loop prints all elements

    <script>        var arr=[                [1,2,3,4,5],                ["A", "B", "C", "D", "E", "F"],                [6,7,8,false,true],        ];        for (Var i=0;i<arr.length;i++) {for            (var j=0;j<arr[i].length;j++) {                alert (Arr[i][j])            }        }    </script>

  

2016-05-29 for loops several applications

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.