V. loop

A, for circulation

The specified number of times execution code block

for (loop variable initialization; cycling conditions; loop variable change (increment or decrement)) {Code} loop

Steps:

1, loop condition is initialized (execution only when the first cycle time)

2, the loop condition is determined, the loop condition is true, step 3, the loop condition is false, then the loop ends

3, the body of code execution cycle

4, loop variable changes

5. The execution of the steps 2, 3, until the end of the loop

Examples: 1, 2, ...... 10 outputs the page

for(var i=0;i<10;i++){

  document.write(i,<br>)

}

Or can be written as ( Declare variables can be put out for recycling, change the loop variable can be placed inside a for loop, however; must be retained )

var i = 0;

for( ; i<10 ; ){

  document.write(i,<br>);

  i++;

}

Note: 1, cycle condition changing + loop variable for controlling the number of times the loop body code execution

Second, double loop for

for(var j=0;j<3;j++){
  for(var i=1;i<6;i++){
    document.write(i,' ')
  }
  document.write('<br>')
}

1, the outer loop is executed for the first, outer loop variable is initialized, the loop condition, the code execution loop, encountered for the inner loop, the loop is finished for the inner, outer for-loop condition is determined to continue, the execution once the outer loop ......

Note: 1, each entry for the inner loop, the loop condition must initialize var i = 1

2, to perform an outer loop for, for the inner loop Once finished, only the outer loop for the next time

3, clearly distinguish between inner and outer loop of code for loop, in general, for the inner loop integral part of the cycle is for the outer loop of code

Third, the use of variables and string concatenation +

var str = 'hello'

str+'world'   //helloworld

Third, create elements

DOM provides a range of ways to use JS to manipulate the DOM node (node ​​creation page, access and other elements, adding elements)

1, create a node var newDiv = document.createElement ( 'span')

2, acquiring element node document.getElementById ( 'body') // assumed to body element id

3, the additive element node body.appenChild ( newDiv )

4, add the class name for the element

newDiv.className='active'

Guess you like

Origin www.cnblogs.com/liankong/p/10927996.html