7 useful jquery Code snippets to share
jquery is a lightweight JavaScript library, one of the most popular client-side HTML scripts, well-known among web designers and developers, and has many useful plugins and technologies to help web developers develop creative and beautiful web pages.
Today we share a few tips for jquery users that will help you to suggest the creative and functional nature of your site layout and application.
First, open the link in a new window
With the following code, you can click on the link to open in a new window:
Author http://www.lai18.com$ (document). Ready (function () {//select All Anchor tags this has http in the href//and AP Ply the Target=_blank $ ("a[href^= ' http ')"). attr (' target ', ' _blank ');});
Two, setting a column of equal height
Apply the following code, you can make your Web application each column height want to wait, reference Source: http://www.lai18.com/content/422703.html
<div class= "Equalheight" style= "Border:1px solid" > <p>first line</p > <p>Second Line</p> <p>Third Line</p></div><div class= "Equalheight" style= "Border:1px solid" > <p>Column Two</p></div> <script src= "Http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js" ></script><script>$ (document). Ready (function () { equalheight ('. Equalheight ');}); /global variable, this will store the highest height valuevar Maxheight = 0;function equalheight (COL) { //Get all the element with class = col col = $ (COL); //loop all the col Col.each (function () { //Store the highest value if ($ (this). Height () > maxheight) { maxheight = $ (This). Height (); } }); //set the height col.height ( MaxHeight);} </script>
Third, jquery preloaded image
This tip can increase the speed at which the page loads pictures:
Author http://www.lai18.comjQuery.preloadImagesInWebPage = function () {for (var ctr = 0; Ctr & lt; arguments.length ; ctr++) {jQuery (""). attr ("src", arguments[ctr]);}} How to use: $.preloadimages ("Image1.gif", "Image2.gif", "image3.gif");//Check whether the picture is loaded $ (' #imageObject '). attr (' src ', ' Image1.gif '). Load (function () {alert (' The image has been loaded ... ');});
Four, disable the right mouse button
Author http://www.lai18.com$ (document). Ready (function () {//catch The right-click context menu $ (document). Bind (" ContextMenu ", function (e) {//warning prompt-optional alert (" No right-clicking! "); Delete the default context menu return false; });});
Five, set the timer
$ (document). Ready (function () {window.settimeout () (function () {//Some code}, 500);});
Vi. counting the number of child elements
<div id= "foo" > <div id= "Bar" ></div> <div id= "baz" > <div id= "Biz" ></div> <span& gt;<span></span></span> </div></div><script src= "http://apps.bdimg.com/libs/ Jquery/1.11.1/jquery.min.js "></script><script type=" Text/javascript ">//jQuery code to Count child Elements $ ("#foo > div"). Size () alert ($ ("#foo > div"). Size ()) </script>
Position the element in the middle of the page
<div id= "foo" style= "Width:200px;height:200px;background: #ccc;" ></div><script src= "Http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js" ></script>< Script type= "text/javascript" >jquery.fn.center = function () {this.css ("position", "absolute"); This.css ("Top", ($ ( window). Height ()-this.height ())/2 + $ (window). scrolltop () + "px"); This.css ("Left", ($ (window). Width ()-this.width ())/2 + $ (window). ScrollLeft () + "px"); return this;} Use the above function as:$ (' #foo '). Center ();</script>
7 useful jquery Code snippets to share