JQuery $.fn.extend ways to customize plugins

Source: Internet
Author: User

The previous example is the tool method for extending jquery, which is used in the form of $.XXX (para). Here's how to extend the jquery object, where any jquery object is accessed.

Specific as follows:

Wyl.js:

1(function($){2     //access Method: $ (' span p '). Siblings (). Chgcolor ();//All ancestor elements are contiguous elements of the P element of span3     //function: Set the color of the jquery object4$.fn.chgcolor =function(colors) {5         varTmpcolor =colors;6         varFlag =!(Tmpcolor);7         if(!(Tmpcolor)) {8             //tmpcolor = ' orange ';9Tmpcolor = ' #93DDFF ';//BlueTen         } One         if(typeofTmpcolor!= ' String '){ AAlert (' Incoming argument must be of type string '); -             return false; -         } the          -$( This). CSS (' background ', Tmpcolor); -     } -}) (JQuery)

Html:

1 <!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">2 <HTMLxmlns= "http://www.w3.org/1999/xhtml">3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />5 <title>Untitled Document</title>6 <Scripttype= "Text/javascript"src= "Http://code.jquery.com/jquery-1.7.2.min.js"></Script>7 <Scripttype= "Text/javascript"src= "Wyl.js"></Script>8 <Script>9 Ten $(function(){ One  A     //$ (' div '). Next (' P '). CSS (' background ', ' red '); -     //$ (' span~p '). Chgcolor ();//Find the P tag for all siblings after the span tag -     //$ (' span~p '). Chgcolor ();//Find the P tag for all siblings after the span tag the     $('span P'). Siblings (). Chgcolor ();//all ancestor elements are adjacent elements of the P element of span -     //$ (' span '). Siblings ('. Haha '). Chgcolor (' orange ');//Find the element of class haha in the same level element of the SPAN element -     //$ (' span '). Siblings (' div '). Chgcolor (' orange ');//Find the element of the span element as a DIV element in all sibling elements -  + }); -  + </Script> A </Head> at  - <Body> - <Div>11111</Div> - <P>11111</P> -  - <Div>22222</Div> in <span>2222</span> - <P>22222</P> to <Pclass= "haha">33333</P> + <span><P>44444</P><BR><Div>I'm a div again</Div></span> - <Div><P>I'm the P element under Div.</P></Div> the <P>55555</P> * <P>6666</P> $ </Body>Panax Notoginseng </HTML>

Effect:

JQuery $.fn.extend ways to customize plugins

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.