Study notes. JavaScript Applications (iii)

Source: Internet
Author: User

1. Function pass value & Probe object

Window.onload = initall;/*function initall () {    for (i=0;i<24;i++) {         var newnum = math.floor (Math.random () *75)   + 1;        document.getelementbyid ("Square"  + i). Innerhtml = newnum;    }}*/function initall () {    if ( document.getElementById) {     //checks whether javascript      is supported by this property    for (i=0;i<24;i++) {             setsquare (i);        }    }     else{        alert ("Sorry,your browser doesn ' t  support this script. ")     }}function setsquare (thissquare) {    var newnum = math.floor (Math.random ()  * 75)  +1;    document.getelementbyid ("Square " + thissquare). Innerhtml = newnum;}

2. Working with arrays

var colplace = new array ( 0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4); Function initall () {    if ( document.getElementById) {        for (i=0;i<24;i++) {             setsquare (i);         }    }    else{         alert ("Sorry,your browser doesn ' t support this scirpt.")     }}function setsquare (thissquare) {    var currsquare  =  "Square"  + thissquare;    var newnum = math.floor ( Math.random ()  * 15)  + 1 + colPlace[thisSquare] * 15;     document.getelementbyid (Currsquare). Innerhtml = newnum;} 

The location of the array definition is either global or within the Setsquare () function.

3.do/while Cycle

Var colplace = new array (0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4);var  Usednums = new array (Function initall) {    if ( document.getElementById) {        for (i=0;i<24;i++) {             setsquare (i);         }    }    else{         alert ("Sorry,your browser doesn ' t support this scirpt.")     }}function setsquare (thissquare) {    var currsquare  =  "Square"  + thisSquare;    var newNum;     Do{        newnum = math.floor (Math.random ()  * 15)  + 1 + colplace[thissquare] * 15; &nBsp;  }while (Usednums[newnum]);    usednums[newnum] = true;     document.getelementbyid (Currsquare). Innerhtml = newnum;}

Note that the loop condition Usednums[newnum] is a false end loop.

4. Dynamically adjust the CSS using JavaScript.

Function setsquare (thissquare) {    var currsquare =  "Square"  +  thisSquare;    var newNum;    do{         newnum = math.floor (Math.random ()  * 15)  + 1 +  Colplace[thissquare] * 15;    }while (Usednums[newnum]);     Usednums[newnum] = true;    document.getelementbyid (currSquare). innerHTML =  newnum;    document.getelementbyid (Currsquare) .classname =  "";     document.getelementbyid (Currsquare). Onmousedown = togglecolor;} Function anothercard () {    for (i=1;i<usednums.length;i++) {         usednums[i] = false;    }    newcard ();     return faLSE;} Function togglecolor (evt) {    if (evt) {         var thissquare = evt.target;        //alert ( Evt.target);    //return object htmltablecellelement         //alert (Evt.type);      //return mousedown    }     else{        var thisSquare =  Window.event.srcelement;    }    if (thisSquare.className ==  "") {        thissquare.classname =  "PickedBG";     }    else{        thissquare.classname  =  "";     }}

5. Check the status.

Function checkwin () {    var winners = new array ( 31,992,15360,507904,541729,557328,1083458,2162820,4329736,8519745,8659472,16252928);     var setsquares = 0;    var winneroption = -1;     //alert (Winners[-1]);     for (i=0;i<24;i++) {         var currSquare =  "Square"  + i;         if (document.getElementById (currsquare) .classname !=  "") {             document.getelementbyid (Currsquare) .className =  "PickedBG"     //when a flashing line is clicked, the remaining squares of the bank are canceled.             setsquares = setsquares  | math.pow (2,i);         //by or operation of the selected clicked grid, i.e. 24-bit binaryThe selected position is set to 1.             //alert (setSquares);         }    }    for (i=0;i<winners.length;i+ +) {        if (setsquares & winners[i])  ==  Winners[i]) {        //The result of winning by matching the operation.             winnerOption = i;             //alert (winneroption);         }    }    if (winnerOption>-1) {         for (i=0;i<24;i++) {             if (Winners[winneroption] & math.pow (2,i)) {         //by matching the selected lattice &NBSP;&NBSP;&NB with the operationsp;             currsquare =  " Square " + i;                 document.getelementbyid (Currsquare) .classname =  "WINNINGBG";             }        }     }}


This article is from the "Technology No Peak" blog, please be sure to keep this source http://shelia.blog.51cto.com/2930476/1622364

Study notes. JavaScript Applications (iii)

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.