Let's take a look at the code first, which is very simple, as follows
Copy Code code as follows:
<div id= "Div1" >
<span>111</span>
<span>222</span>
<span>333</span>
<button id= "Button1" >clear</button>
</div>
Copy Code code as follows:
$ (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. The following figure
2. If the $ ("#div1 span") is executed. html (), only the value of the first object in the array is taken
So if the selector gets an array, it's best to use each () when you want to operate on each element of the group.
There are other things to note.
A note selector containing a special symbol in the selector contains a ".", "#", "(" or "]" special characters, which are not included in the attribute value according to the requirements of the world's consortium, are occasionally encountered in an expression containing "#" and "." Such special characters, if the normal way to deal with the words will be wrong.
The way to resolve this type of error is to escape with an escape character.
<div id= "id#b" >bb</div>
<div id= "id[1]" >cc</div>
Can't write like this:
$ (' #id #b '); $ (' #id [1] ');
You should use the escape symbol:
$ (' #id \ #b '); Escape special Character "#"
$ (' #id \\[1\\] '); Escape special Characters "[]"