HTML, text, Val callback functions in jquery

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.