Article From:https://segmentfault.com/q/1010000011703360
Question:
 //If it is a HTML string, it is converted to HTML element'< div> < p> 123< /p> < /div> 'If ($.isHTML(selector)) {Var div = document.createElement ('div'), node, result = [];DiV.innerHTML = selector;/ / get all the child elements of div***for (node = div.firstChild; node; no)De.nextSibling) * * * *If (node.nodeType = = = 1) {Ret.push (node);}}}The general cycle is not var i=0, i< xxx.length; i++.What does this cycle mean? Selector is the element in $('selector').What is the initial value of this cycle? What is the condition of circulation? Where to end? How to go + +?

Answer 0:

MDNDetailed explanation

First sentence
An expression (including assignment statement) or variable declaration. It is typically used to initialize a counter.
Second sentences
A conditional expression is used to determine whether each cycle can be executed. If the result of this expression is true, statement will be executed.
Third sentences
The expression to be executed at the end of each cycle.

There is no statement that a counter must be declared. Sentence two must be of a comparative length. Statement three must be increased or decremented.

Answer 1:

The first one is the child of the div, the middle one is whether the brother of the node exists, and the last one is the brother of the node.
It means that the brothers who have been circulating node know that node has no brothers.
Hope to adopt

Answer 2:

for Statement is used to create a loop that contains three optional expressions enclosed in parentheses separated by semicolons followed by a statement executed in the loop (usually a block statement).

grammar

for ([initialization]; [condition]; [final-expression])
statement

initialization
An expression (including assignment statement) or variable declaration. It is typically used to initialize a counter. The expression can declare new variables using the VaR keyword. The initialized variable is not a local variable of the loop, but is in the same scope as the for loop. The knot of the expressionFruit is meaningless.
condition
A conditional expression is used to determine whether each cycle can be executed. If the result of this expression is true, statement will be executed. This expression is optional. If it is ignored, it is considered to be true forever. If the result is false, the execution process will beJump to the first statement after the for statement structure.
final-expression
The expression to be executed at the end of each cycle. The execution time is before the next condition calculation. It is usually used to update or incrementally counter variables.
statement
Statements that are executed as long as the result of condition is true. To execute multiple statements in the loop body, a block statement ({…}) is used to contain the statement to be executed. There is no statement to execute, using an empty statement (()).

More

The main code should be incomplete, but I believe you will understand this after reading.

Answer 3:

node = div.firstChild; node; node.nextSibling

1 node = div.firstChild # Initialization Node is bound to the first child of Div.
2 node; #Whenever node exists, it means that the loop continues.
3 node.nextSibling # Whenever a cycle is completed, Node is bound to the next sibling element.

That is, the meaning of this loop is to loop from the first child element of a div to the last child element and Node is equal to the child element to which the loop goes.

Answer 4:
Let's take a look at the instructions on the MDN: node. nextSibling is a read-only property that returns the node immediately following in its parent's childNodes list, and null if the specified node is the last node.Say it again, you.The third expression for (node = Div. firstChild; node; node. nextSibling) is problematic in that it only gets the next sibling node of the node, and does not assign a value to the node. It should be changed to foR (node = div.firstChild; node; node = node.nextSibling) is so easy to understand.First, node is initialized to the first child of div to determine whether node is n.Ull or underfined, execute the loop body inside for if it is not empty, then let the node become its next sibling node, and then judge whether the node is null or not, and then execute the loop body again if it is not null, and then repeat... When node is div child nodeWhen the last node is in, the next time you go back to null, you jump out of the loop.

Answer 5:
var node;
// Judge whether the node value is "," null, undefined?/ / do many people really judge?If (node = = "" node "= = null = node =" undefined ") {/ /...}/ / in fact, it is not necessary to judge whether node is valuable or not undefined./ / look at the following example.If (node) {/ / that's so simple.}If LZ can understand thisThere is no doubt about the for above.

Answer 6:

It is equivalent to

for(var i=0,len=node.parentNode.childNodes.length;i<len;i++){
    if(node.parentNode.childNodes[i].nodeType===1){
        ret.push(node);
    }
}

Why do you write that? Please refer to the upstairs.

Leave a Reply

Your email address will not be published. Required fields are marked *