Let's take a look at the code first, which is simple, as follows:
/*html Part */
<div id= "Div1" >
<span>111</span>
<span>222</span>
<span>333</span>
<button id= "Button1" >clear</button>
</div>
/*jquery Part */
$ (function () {
$ ("#button1"). Click (function () {
$ ("#div1 span"). HTML ("AAA");
});
});
$ ("#div1 span") gets an array of three objects
1. If you execute $ ("#div1 span"). HTML ("AAA"), all objects within the array will change.
2. If you execute $ ("#div1 span"). HTML (), only the value of the first object of the array is taken.
So if the selector gets an array, it's a good idea to use each () when you want to manipulate each element of a group.
There are some other considerations
Note Selector with special symbols in selectors the selectors contain special characters such as ".", "#", "(" "or"] "in accordance with the rules of the world, the attribute value cannot contain these special characters, but in the actual project occasionally encountered expressions containing" # "and". " such as special characters, if the normal way to deal with it will be wrong.
The way to resolve such errors is to escape with escape characters.
<div id= "id#b" >bb</div>
< div id= "id[1" >cc</div>
Cannot write like this:
$ (' #id #b '); $ (' #id [1] ');
The escape symbol should be used:
$ (' #id \ #b '); Escape special Characters "#"
$ (' #id \\[1\\] '); Escape special Characters "[]"
HTML () in jquery (reproduced)