Chained Programming
1. Benefits
"In a word, chained programming can save a lot of repetitive code. ”
the words What do you mean? Let me give you an example.
/* Set two properties of the Obj object */
The common practice is this
Obj.name = ' Xiao Ming ';
Obj.age = 155;
Chained programming is the case.
Obj.name (' Xiao Ming '). Age (15);
2. Note
Chained Programming Note:
$ (' div '). html (' Set Value '). Val (' Set value '); this is possible.
But $ (' div '). html (). Text () is wrong, because getting a value returns a string instead of the object itself, so you can't chain-program.
3. broken chain of the whole
Chain programming must pay attention to when the "destruction" of the chain, the current object delivery, if the transmission is broken, then continue to chain programming will have unexpected results! Nextall (), Prevall (), Sibilings ()
How to resolve: Call the End () method. (Returns the object before the chain is destroyed.) )
Implicit Iteration
In the example above
$ (' #btn '). Click (function () {
Implicit iteration:
$ (' P '). Text (' for New China '). CSS (' background ', ' blue ');
});
Gets the element p, getElementsByTagName found in the traditional DOM notation that no method name elements is a plural form.
In other words, a collection should be obtained.
We know the properties of the elements in the set, the necessary iterations. (It's just a walk.)
But there is no need for traversal in jquery to be set directly. Why is it? This is the implicit iteration chant.
What does the chain programming of jquery mean by implicit iteration?