26 Jquery Usage Tips (worth collecting) __jquery

Source: Internet
Author: User
Here's a list of some of the jquery usage tips. For example, there is no right click, hide search text box text, open link in new window, detect browser, preload picture, page style switch, all columns, dynamic control page font size, get x y value of mouse pointer, verify element is empty, replace element, delay load, Verify that the elements exist in the jquery collection, make Div clickable, clone objects, center elements, count elements, use the jquery class library on Google hosts, disable jquery effects, and resolve conflicts between jquery class libraries and other JavaScript class libraries.

1. No right-click [JavaScript] view plain copy print? $ (document). Ready (function () {$ (document). Bind ("ContextMenu", function (e) {return false;};});

$ (document). Ready (function () {
     $ (document). Bind ("ContextMenu", function (e) {return
         false;
     };
 }); 

2. Hide search text box text [JavaScript]View Plain copy print? $ (document). Ready (function () {$ ("Input.text1"). Val ("Enter Your search text here"); Textfill ($ (' Input.text1 ');}); function Textfill (input) {//input focus text function var OriginalValue = Input.val (); Input.focus (function () {if ($.trim (Input.val ()) = = OriginalValue) {input.val (');}}); Input.blur (function () {if ($.trim (Input.val ()) = = ') {input.val (OriginalValue);}}); }
$ (document). Ready (function () { 
$ ("Input.text1"). Val ("Enter Your search text here");   
	Textfill ($ (' input.text1 ')
     ); function Textfill (input) {//input focus text function
     var originalvalue = Input.val ();
     Input.focus (function () {
         if ($.trim (Input.val ()) = = OriginalValue) {input.val (');} 
    });
     Input.blur (function () {
         if ($.trim (Input.val ()) = = ') {input.val (originalvalue);}
     });
 
3. Open a link in a new window

[JavaScript] view plain copy print? $ (document). Ready (function () {//example 1:every Link would open in a new window $ (' a[href^= ' http://"] '). attr (" Target "," _ Blank "); Example 2:links with the rel= "external" attribute would only be open in a new window $ (' a[@rel $= ' External ']). Click (functio N () {this.target = "_blank";}); }); How to use <a href= "http://www.opensourcehunter.com" Rel=external>open link</a>

$ (document). Ready (function () {
    //example 1:every Link would open in a new window
    $ (' a[href^= ' http://"]"). attr (" Target "," _blank ");

    Example 2:links with the rel= "external" attribute would only be open in a new window
    $ (' a[@rel $= ' External ']). Click (Fun Ction () {
       this.target = "_blank";
    });
 };
 
How to use
 


4. Detection browser

Note: In version jquery 1.4, $.support replaces the $.browser variable
[JavaScript] view plain copy Prin T? $ (document). Ready (function () {//Target Firefox 2 and above if ($.browser.mozilla && $.browser.version >= "1.8" {//do something}//target Safari if ($.browser.safari) {//do something}//target Chrome if ($.browser.chrome) {/ /do something}//target Camino if ($.browser.camino) {//Does something}//target Opera if ($.browser.opera) {//Do som Ething}//target IE6 and below if ($.browser.msie && $.browser.version <= 6) {//do something}//target an Ything above IE6 if ($.browser.msie && $.browser.version > 6) {//Do something}});

$ (document). Ready (function () {
 //Target Firefox 2 and above
 if ($.browser.mozilla && $.browser.version >= "1.8") {
     //do something
 }

   //Target Safari
 if ($.browser.safari) {
     //do something
 }

   Target Chrome
 if ($.browser.chrome) {
     //do something
 }

   //target Camino
 if ($. Browser.camino) {
     //do something
 }

   //Target Opera
 if ($.browser.opera) {
     //do something
 }
 
  Target IE6 and below
 if ($.browser.msie && $.browser.version <= 6) {
     //do something
 }
   
    / /Target Anything above IE6
 if ($.browser.msie && $.browser.version > 6) {
     //do something
 }
  }); 



5. Pre-loading pictures

[JavaScript] view plain copy print? $ (document). Ready (function () {jquery.preloadimages = function () {for (var i = 0; i<arguments. LENGTH; JQuery (? ). attr ("src", arguments[i]); }//How to use $.preloadimages ("image1.jpg"); });

$ (document). Ready (function () {
 jquery.preloadimages = function ()
 {for
   (var i = 0; i<arguments. LENGTH; JQuery (? ). attr ("src", arguments[i]);
   }
 How to use
 $.preloadimages ("image1.jpg"); 


6. Page style Toggle [JavaScript] view plain copy print $ (document). Ready (function () {$ ("a. Styleswitcher "). Click (function () {//SWICTH the LINK REL attribute with the value in A REL attribute $ (' Link[rel=styleshee T] '. attr (' href ', $ (this). attr (' rel ')); }); Your header <link rel=stylesheet type=text/css href= ' default.css ' >//Place ' the links <a Class=styleswitcher href= "#" Rel=default.css>default theme</a> <a class=styleswitcher href= "#" rel= red.css>red theme</a> <a class=styleswitcher href= "#" Rel=blue.css>blue Theme</A>});

$ (document). Ready (function () {
     $ ("A.styleswitcher"). Click (function () {
         //swicth the LINK REL attribute with The value in A REL attributes
         $ (' link[rel=stylesheet] '). attr (' href ', $ (this). attr (' REL ');
     });
 your header
 <link rel=stylesheet type=text/css href= "Default.css" >//The
 Links
 <a class=styleswitcher href= "#" Rel=default.css>default theme</a> <a class=
 Styleswitcher href= "#" rel=red.css>red theme</a>
 <a class=styleswitcher href= "#" rel=blue.css> Blue theme</a>
 });


7. Column height is the same

If you use two CSS columns, this can be the same height as the two columns.

[JavaScript] view plain copy print? $ (document). Ready (function () {function equalheight (group) {tallest = 0; Group.each (function () {Thisheight = $ (this). Hei Ght (); if (Thisheight > Tallest) {tallest = Thisheight;}}); Group.height (tallest); //How to use $ (document). Ready (function () {Equalheight ($ ('. Left ')); Equalheight ($ (". Right")); });

$ (document). Ready (function () {
   function equalheight (group) {
     tallest = 0;
     Group.each (function () {
         Thisheight = $ (this). Height ();
         if (Thisheight > tallest) {
             tallest = Thisheight;
         }
     });
     Group.height (tallest);
 }
 How do you
 $ (document). Ready (function () {
     equalheight ($ (". Left"));
     Equalheight ($ (". Right"));};
 


8. Dynamically control page font size

User can change page font size

[JavaScript] view plain copy print $ (document). Ready (function () {//Reset the font size (back to de fault) var originalfontsize = $ (' html '). CSS (' font-size '); $ (". Resetfont"). Click (function () {$ (' html '). CSS (' font-size ', originalfontsize);}); Increase the font size (bigger font0 $ (". Increasefont"). Click (function () {var currentfontsize = $ (' html '). CSS (' Font-size '); var currentfontsizenum = parsefloat (currentfontsize, 10); var newfontsize = currentfontsizenum*1.2; $ (' HTML '). CSS (' font-size ', newfontsize); return false; }); Decrease the font size (smaller font) $ (". Decreasefont"). Click (function () {var currentfontsize = $ (' html '). CSS (' Font-size '); var currentfontsizenum = parsefloat (currentfontsize, 10); var newfontsize = currentfontsizenum*0.8; $ (' HTML '). CSS (' font-size ', newfontsize); return false; }); });

$ (document). Ready (function () {
   //Reset the font size (back to default)
   var originalfontsize = $ (' html '). CSS (' Font-size ');
     $ (". Resetfont"). Click (function () {
     $ (' HTML '). CSS (' font-size ', originalfontsize);
   });
   Increase the font size (bigger font0
   $ (". Increasefont"). Click (function () {
     var currentfontsize = $ (' html '). CSS (' font-size ');
     var currentfontsizenum = parsefloat (Currentfontsize, ten);
     var newfontsize = currentfontsizenum*1.2;
     $ (' HTML '). CSS (' font-size ', newfontsize);
     return false;   });
   Decrease the font size (smaller font)
   $ (". Decreasefont"). Click (function () {
     var currentfontsize = $ (' HTML ') . css (' font-size ');
     var currentfontsizenum = parsefloat (Currentfontsize, ten);
     var newfontsize = currentfontsizenum*0.8;
     $ (' HTML '). CSS (' font-size ', newfontsize);
     return false;
   });
  
 
 


9. Return to the top of the page function

[JavaScript] view plain copy print $ (document). Ready (function () {$ (' a[href*=#] '). Click (function () {if (Location.pathname.replace (/^\//, ') = This.pathname.replace (/^\//, ') && location.hostname = = This.hostname) {var $target = $ (this.hash); $target = $target. Length && $target | | $ (' [name= ' + this.hash.slice (1 ) +']'); if ($target. length) {var targetoffset = $target. Offset (). Top; $ (' html,body '). Animate ({Scrolltop:targetoffset}, 900); Eturn false; } } }); How do I//where you want to scroll to <a name=top></a>//The link <a href= "#top" >go t o top</a>});

$ (document). Ready (function () {
 $ (' a[href*=#] '). Click (function () {
  if (location.pathname.replace (/^\//, ') = = This.pathname.replace (/^\//, ')
  && location.hostname = = this.hostname) {
    var $target = $ (This.hash) ;
    $target = $target. Length && $target
    | | $ (' [name= ' + this.hash.slice (1) + '] ');
    if ($target. length) {
   var targetoffset = $target. Offset (). Top;
   $ (' html,body ')
   . Animate ({Scrolltop:targetoffset}, 900);
     return false;}}
   );
 How do I//place this
 where you want to scroll to
 <a name=top></a>
 //the link
 <a href= "#top" >go to Top</a>
 }); 


11. Get the mouse pointer xy value

[JavaScript] view plain copy print? $ (document). Ready (function () {$ (). MouseMove (function (e) {//display the x and Y axis values inside the div with the ID XY $ (' #XY '). HTML ("X Axis:" + E.pagex + "| Y Axis "+ e.pagey);}"; How to use <div id=xy></div>});

$ (document). Ready (function () {
    $ (). MouseMove (function (e) {
      //display the x and Y axis values inside the div with th E ID XY
     $ (' #XY '). HTML ("X Axis:" + E.pagex + "| Y Axis "+ e.pagey);
   }"; How to use
 <div id=xy></div>
   


12. Verify that the element is empty

[JavaScript] view plain copy print? $ (document). Ready (function () {if ($ (' #id '). HTML ()) {//Do something}});

$ (document). Ready (function () {
   if ($ (' #id '). HTML ()) {
    //do Something
    }
 });


13. Replace Elements

[JavaScript] view plain copy print? $ (document). Ready (function () {$ (' #id '). ReplaceWith (' <div>i have been replaced</div> ');});

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.