Literacy First:
Excerpt from Rookie Tutorial: JQuery method: Text (), HTML (), and Val () have callback functions.
The callback function has two parameters: the subscript of the current element in the selected element list, and the original (old) value. Then return the string you want to use with the new value of the function
Then the code:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"></Head><Body> <ul> <Li>Demo</Li> <Li>Demo</Li> <Li>Demo</Li> <Li>Demo</Li> </ul></Body><Scriptsrc= "Jquery-1.12.4.min.js"></Script><Script> $("Li"). Click (function () { $( This). HTML (function(I,origintex) {return(Origintex+i); }) })</Script></HTML>
Strangely, when clicking on the element "Li" always appends the subscript 0 to the old text, which is not the same as expected.
Then look at the following code:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"></Head><Body> <ul> <Li>Demo</Li> <Li>Demo</Li> <Li>Demo</Li> <Li>Demo</Li> </ul></Body><Scriptsrc= "Jquery-1.12.4.min.js"></Script><Script>//$ ("Li"). Click (function () {//$ (this). HTML (function (I,origintex) {//return (origintex+i);// })// })$ (document). Click (function(){ $("Li"). HTML (function(i,origintxt) {$ ( This). html (origintxt+i); }) })</Script></HTML>
Change the source of the event, the expected results appear.
"Code parsing"
The first case that triggers the event is the LI element that currently triggers the Click event, and $ (this) points to it with only one element, so its subscript is always 0;
The second case is simply to trigger the event via the Document object, and then perform a typical traversal assignment, which does not explain much.
HTML, text, Val callback functions in jquery