七個不允許錯過的jQuery小技巧
jQuery是一款輕量級的JavaScript庫,是最流行的客戶端HTML腳本之一,它在WEB設(shè)計師和開發(fā)者中非常的有名,并且有非常多有用的插件和技術(shù)。
本文我們將為大家分享一些jQuery小技巧:
一、在新窗口打開鏈接
用下面的代碼,你點擊鏈接即可在新窗口打開:
$(document).ready(function() { //select all anchor tags that have http in the href //and apply the target=_blank $("a[href^='http']").attr('target','_blank'); });
二、設(shè)置等高的列
應(yīng)用下面的代碼,可以使得你的WEB應(yīng)用每一列高度都想等:
<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 value var 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>
三、jQuery預(yù)加載圖像
這個小技巧可以提升頁面加載圖片的速度:
jQuery.preloadImagesInWebPage = function() { for (var ctr = 0; ctr & lt; arguments.length; ctr++) { jQuery("").attr("src", arguments[ctr]); } } // 使用方法: $.preloadImages("image1.gif", "image2.gif", "image3.gif"); // 檢查圖片是否被加載 $('#imageObject').attr('src', 'image1.gif').load(function() { alert('The image has been loaded…'); });
四、禁用鼠標右鍵
$(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; }); });
五、設(shè)定計時器
$(document).ready(function() { window.setTimeout(function() { // some code }, 500); });
六、計算子元素的個數(shù)
<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>
七、把元素定位到頁面中間
<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>
讀到這里的朋友就知道你沒有錯過學(xué)習(xí)的機會,把這些小技巧積累起來,一定會幫助大家設(shè)計出有創(chuàng)意和精美的Web頁面。
相關(guān)文章
jQuery基于cookie實現(xiàn)的購物車實例分析
這篇文章主要介紹了jQuery基于cookie實現(xiàn)購物車的方法,結(jié)合實例形式簡單分析了jQuery基于cookie實現(xiàn)針對商品信息的購物車存儲功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12jQuery配合coin-slider插件制作幻燈片效果的流程解析
coin-slider是GitHub上的一個人氣JavaScript開源插件,用它來實現(xiàn)幻燈片效果只需要動手寫很少的代碼,下面我們就來看一下jQuery配合coin-slider插件制作幻燈片效果的流程解析.2016-05-05jQuery動態(tài)添加及刪除表單上傳元素的方法(附demo源碼下載)
這篇文章主要介紹了jQuery動態(tài)添加及刪除表單上傳元素的方法,涉及jQuery的事件動態(tài)綁定及頁面元素動態(tài)操作技巧,并附帶了demo源碼供讀者下載參考,代碼非常簡潔實用,需要的朋友可以參考下2016-01-01基于jQuery實現(xiàn)點擊同時更改兩個iframe的網(wǎng)址
最近寫了兩個管理后臺的前端頁面,其中有一個管理后臺,左側(cè)菜單導(dǎo)航和右側(cè)內(nèi)容頁是兩個iframe,需求是,點擊上面的主導(dǎo)航時,左側(cè)iframe和右側(cè)iframe調(diào)用不同的鏈接.2010-07-07