Jquery1.4 tutorial 1 convenient setting functions

Source: Internet
Author: User

Convenient setting functions
For most of the jquery1.4 setting values, you can set the second parameter as the callback function. For example:. css (),. attr (),. val (),. html (),. text ().
This is very abstract, let's look at the example http://demo.jb51.net/js/jquery1.4/jquery_1.htm
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead> <meta http-equiv =" content-Type "content =" text/html; charset = gb2312 "/> <title> convenient setting function </title> <link href =" basic.css "rel =" stylesheet "type =" text/css "/> <link href = "demo.css" rel = "stylesheet" type = "text/css"/> </pead> <body> Improvement 1: For most of the jquery1.4 setting values, you can set the second parameter as the callback function. For example:. css (),. attr (),. val (),. html (),. text (). <P> </p> View the demo below <p> This is a link. </P> <p> use the callback function to directly replace the text in the link. The previous method is to first take the htnl of a, then use regular expression matching, and finally replace it. </P> <p> Step 1: Let's look at the code: </p> <code> Functions ('{a1'{.html (function (I, html) {return html. replace (/. /G ,'! Replaced ');}); </code> <input name = "btn1" id = "btn1" type = "button" value = "Click here to replace"/> you will find that ". "Replaced "! Already replaced ". </P> <p> is it more convenient than before? </P> <p> it is also applicable to other setting value pairs, such as attr (). You can change the second parameter to a function. </P> <p> the title attribute of this replacement link: </p> <code >$ ('# a1 '). attr ("title", function (I, title) {return title + "(already replaced )";}); </code> </p> <input name = "btn2" id = "btn2" type = "button" value = "Click here to replace"/> </p> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The code to replace text with an object before 1.4 is as follows:
Copy codeThe Code is as follows:
<A href = "" id = "a1" title = "test"> This is a link. </A>
Var _ html = ('{a1'{.html ();
Var _ newHtml = html. replace (/. /G ,'! Replaced ');
('{A1'{.html (_ newHtml );

And 1.4 as long as this:
Copy codeThe Code is as follows:
Function (I, html) {return html. replace (/. /G ,'! Replaced ');});

One line of code! It is important to traverse multiple objects and process them separately.
Let's take a look at the two parameters of the callback function:
The first parameter is the index value, which is useful when processing the traversal objects separately.
The second parameter is the old html value.
This callback function must have a return value!
This processing method can be applied not only to html (), but also to other setting value pairs. The detailed list is as follows:
. Css (),. attr (),. val (),. text (),. append (),. prepend (),. before (),. after (),. replaceWith (),. wrap (),. wrapInner (),. offset (),. addClass (),. removeClass (),. toggleClass ()
In the next phase, we will focus on the improvement of jquery1.4 in ajax.

Related Article

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.