7個(gè)有用的jQuery代碼片段分享
jQuery是一款輕量級(jí)的JavaScript庫,是最流行的客戶端HTML腳本之一,它在WEB設(shè)計(jì)師和開發(fā)者中非常的有名,并且有非常多有用的插件和技術(shù)幫助WEB開發(fā)人員開發(fā)出有創(chuàng)意和漂亮的WEB頁面。
今天我們?yōu)閖Query用戶分享一些小技巧,這些技巧將幫助你提示你網(wǎng)站布局和應(yīng)用的創(chuàng)意性和功能性。
一、在新窗口打開鏈接
用下面的代碼,你點(diǎn)擊鏈接即可在新窗口打開:
$(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è)小技巧可以提升頁面加載圖片的速度:
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>
七、把元素定位到頁面中間
<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>
相關(guān)文章
jQuery easyui datagrid動(dòng)態(tài)查詢數(shù)據(jù)實(shí)例講解
接下來將從前臺(tái)提交查詢條件,從MSSQL返回json數(shù)據(jù)的一個(gè)事例來講解一下datagrid動(dòng)態(tài)查詢數(shù)據(jù),感興趣的你可不要錯(cuò)過了哈,希望本文可以幫助到你2013-02-02
javascript(基于jQuery)實(shí)現(xiàn)鼠標(biāo)獲取選中的文字示例【測(cè)試可用】
這篇文章主要介紹了javascript(基于jQuery)實(shí)現(xiàn)鼠標(biāo)獲取選中的文字,涉及jQuery響應(yīng)鼠標(biāo)事件及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-10-10
JQuery中attr屬性和jQuery.data()學(xué)習(xí)筆記【必看】
下面小編就為大家?guī)硪黄狫Query中attr屬性和jQuery.data()學(xué)習(xí)筆記【必看】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過來看看吧2016-05-05
jquery 動(dòng)態(tài)增加,減少input表單的簡(jiǎn)單方法(必看)
下面小編就為大家?guī)硪黄猨query 動(dòng)態(tài)增加,減少input表單的簡(jiǎn)單方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
jQuery.position()方法獲取不到值的安全替換方法
這篇文章主要介紹了jQuery.position()方法獲取不到值的安全替換方法,本文給出了一種變通的方法,用.offset()來換算,需要的朋友可以參考下2015-03-03
jQuery獲得IE版本不準(zhǔn)確webbrowser的解決方法
用$.browser.version經(jīng)常出現(xiàn)一些不準(zhǔn)確的狀況,最近碰到,做了一些總結(jié),不知道是否全面2014-02-02
javascript 歷史記錄 經(jīng)常用于產(chǎn)品最近歷史瀏覽
在很多購物網(wǎng)站,都有“產(chǎn)品的瀏覽歷史記錄”,這是個(gè)很貼心的功能。2009-05-05
淺談EasyUI中Treegrid節(jié)點(diǎn)的刪除
本文給大家介紹的是EasyUI中樹節(jié)點(diǎn)(Treegrid節(jié)點(diǎn))的刪除的方法和示例,這里推薦給大家,希望對(duì)大家能有所幫助。2015-03-03

