[Add to favorites] 7 useful jQuery code snippets, jquery snippets
Seven useful jQuery code snippets
JQuery is a lightweight JavaScript library and one of the most popular Client HTML scripts. It is very famous among WEB designers and developers, there are also many useful plug-ins and technologies to help WEB developers develop creative and beautiful WEB pages.
Today, we will share some tips for jQuery users. These tips will help you to demonstrate the creativity and functionality of your website layout and applications.
1. Open the link in a new window
With the following code, you can click the link to open it in a new window:
//author http://www.lai18.com$(document).ready(function() { //select all anchor tags that have http in the href //and apply the target=_blank $("a[href^='http']").attr('target','_blank');});
2. Set high-level Columns
The following code can be used to make the height of each column of your WEB application wait. For details, refer:
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>
Iii. jQuery pre-Loaded Images
This tip can speed up page image loading:
// Author http://www.lai18.comjQuery.preloadImagesInWebPage = function () {for (var ctr = 0; ctr & lt; arguments. length; ctr ++) {jQuery (""). attr ("src", arguments [ctr]) ;}// usage: $. preloadImages ("image1.gif", "image2.gif", "image3.gif"); // check whether the image is loaded $ ('# imageobject '). attr ('src', 'image1.gif '). load (function () {alert ('the image has been loaded... ');});
4. Right-click Disabled
//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; });});
5. Set the timer
$(document).ready(function() { window.setTimeout(function() { // some code }, 500);});
6. Calculate 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>
7. Locate 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>