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