7 useful jquery Code snippets to share

Source: Internet
Author: User

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 Apply the Target=_blank  $ ("a[href^= ' http ']"). attr (' target ', ' _blank ');});


second, set the column of equal height

By applying the following code, you can make every column of your Web app feel like:
<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 would store the highest height valuevar maxheight = 0;function equalheight (col) {//get all the Elem Ent with class = col col = $ (col); Loop all the Col col.each (function () {  //store the highest value  if ($ (this). Height () > MaxHeight) {   maxh Eight = $ (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
$ (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><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>


Reference Source:
7 useful jquery Code snippets to share
Http://www.lai18.com/content/422703.html

7 useful jquery Code snippets to share

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.