七個(gè)不允許錯(cuò)過(guò)的jQuery小技巧
jQuery是一款輕量級(jí)的JavaScript庫(kù),是最流行的客戶(hù)端HTML腳本之一,它在WEB設(shè)計(jì)師和開(kāi)發(fā)者中非常的有名,并且有非常多有用的插件和技術(shù)。

本文我們將為大家分享一些jQuery小技巧:
一、在新窗口打開(kāi)鏈接
用下面的代碼,你點(diǎn)擊鏈接即可在新窗口打開(kāi):
$(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ù)加載圖像
這個(gè)小技巧可以提升頁(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…');
});
四、禁用鼠標(biāo)右鍵
$(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è)定計(jì)時(shí)器
$(document).ready(function() {
window.setTimeout(function() {
// some code
}, 500);
});
六、計(jì)算子元素的個(gè)數(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>
七、把元素定位到頁(yè)面中間
<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>
讀到這里的朋友就知道你沒(méi)有錯(cuò)過(guò)學(xué)習(xí)的機(jī)會(huì),把這些小技巧積累起來(lái),一定會(huì)幫助大家設(shè)計(jì)出有創(chuàng)意和精美的Web頁(yè)面。
- jQuery中的一些小技巧
- 15個(gè)jquery常用方法、小技巧分享
- jQuery提交多個(gè)表單的小技巧
- jquery操作復(fù)選框(checkbox)的12個(gè)小技巧總結(jié)
- Jquery下的26個(gè)實(shí)用小技巧(jQuery tips, tricks & solutions)
- jQuery前端開(kāi)發(fā)35個(gè)小技巧
- WEB前端開(kāi)發(fā)都應(yīng)知道的jquery小技巧及jquery三個(gè)簡(jiǎn)寫(xiě)
- 幾個(gè)比較經(jīng)典常用的jQuery小技巧
- jQuery開(kāi)發(fā)者都需要知道的5個(gè)小技巧
- 前端開(kāi)發(fā)必知的15個(gè)jQuery小技巧
相關(guān)文章
jquery異常問(wèn)題Uncaught?TypeError:?$(...).on?is?not?a?funct
這篇文章主要介紹了jquery異常問(wèn)題Uncaught?TypeError:?$(...).on?is?not?a?function,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
JQuery中form驗(yàn)證出錯(cuò)信息的查看方法
JQuery中form驗(yàn)證出錯(cuò),可以采用以下方式來(lái)查看具體input的出錯(cuò)信息,下面有個(gè)不錯(cuò)的示例,有類(lèi)似情況的朋友可以參考下2013-10-10
設(shè)置jQueryUI DatePicker默認(rèn)語(yǔ)言為中文
本文主要介紹jQueryUI DatePicker設(shè)置中文的方法,需要的朋友可以參考下。2016-06-06
jQuery基于BootStrap樣式實(shí)現(xiàn)無(wú)限極地區(qū)聯(lián)動(dòng)
這篇文章主要介紹了jQuery基于BootStrap樣式實(shí)現(xiàn)無(wú)限極地區(qū)聯(lián)動(dòng)的相關(guān)資料,需要的朋友可以參考下2016-08-08
jquery實(shí)現(xiàn)select選擇框內(nèi)容左右移動(dòng)代碼分享
這篇文章主要介紹了jquery實(shí)現(xiàn)select選擇框內(nèi)容左右移動(dòng)代碼,感興趣的小伙伴們可以參考一下2015-11-11
jQuery實(shí)現(xiàn)選項(xiàng)卡切換圖片
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)選項(xiàng)卡切換圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
jQuery實(shí)現(xiàn)的老虎機(jī)跑動(dòng)效果示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的老虎機(jī)跑動(dòng)效果,涉及jQuery事件響應(yīng)以及結(jié)合時(shí)間函數(shù)動(dòng)態(tài)操作頁(yè)面元素相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-12-12

