欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

分享12個(gè)實(shí)用的jQuery代碼片段

 更新時(shí)間:2016年03月09日 09:16:46   作者:極道先生  
這篇文章主要介紹了12個(gè)實(shí)用的jQuery代碼片段,本文給出了在新窗口打開(kāi)鏈接、設(shè)置等高的列、jQuery預(yù)加載圖像、禁用鼠標(biāo)右鍵、設(shè)定計(jì)時(shí)器等實(shí)用代碼片段,需要的朋友可以參考下

jQuery是一款優(yōu)秀的JavaScript庫(kù),它在WEB開(kāi)發(fā)者和網(wǎng)頁(yè)設(shè)計(jì)師中非常出名,幫助網(wǎng)頁(yè)設(shè)計(jì)師開(kāi)發(fā)出很多有創(chuàng)意和漂亮的WEB頁(yè)面。

本文主要分享了12個(gè)有用的jQuery技巧,具體內(nèi)容如下

下面是我收集的一些小技巧,這些技巧將幫助你提高你網(wǎng)站布局和應(yīng)用的創(chuàng)意性以及功能性。

一、在新窗口打開(kāi)鏈接

用這個(gè)代碼,你點(diǎn)擊超文本鏈接時(shí)會(huì)在新窗口中打開(kāi),為用戶(hù)帶來(lái)更好的體驗(yà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è)定計(jì)時(shí)器

$(document).ready(function() {
 window.setTimeout(function() {
 // some code
 }, 500);
});

 

三、設(shè)置等高的列

使用下面的代碼,可以使得你的網(wǎng)頁(yè)應(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è)技巧可以加快網(wǎng)頁(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…');
});

 五、把元素定位到頁(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>
 

六、禁用鼠標(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;
 });
});

 七、計(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>

 八、更改樣式列表

這段代碼將幫助你更改樣式列表。

 $(document).ready(function() {
 $("a.cssSwap").click(function() { 
 //swap the link rel attribute with the value in the rel 
 $('link[rel=stylesheet]').attr('href' , $(this).attr('rel')); 
 }); 
 });

 九、使用jQuery設(shè)定文本大小

加入這段代碼,用戶(hù)可根據(jù)需求重新設(shè)定文本尺寸(增加或減少)。

 $(document).ready(function() {
 //find the current font size
 var originalFontSize = $('html').css('font-size');

//Increase the text size
 $(".increaseFont").click(function() {
 var currentFontSize = $('html').css('font-size');
 var currentFontSizeNumber = parseFloat(currentFontSize, 10);

var newFontSize = currentFontSizeNumber*1.2;
 $('html').css('font-size', newFontSize);
 return false;
 });

//Decrease the Text Size
 $(".decreaseFont").click(function() {
 var currentFontSize = $('html').css('font-size');
 var currentFontSizeNum = parseFloat(currentFontSize, 10);

var newFontSize = currentFontSizeNum*0.8;
 $('html').css('font-size', newFontSize);
 return false;
 });

// Reset Font Size
 $(".resetFont").click(function(){
 $('html').css('font-size', originalFontSize);
 });
 });

十、檢測(cè)當(dāng)前鼠標(biāo)坐標(biāo)

使用這個(gè)JS代碼,你可以在任何瀏覽器里獲取鼠標(biāo)坐標(biāo)。

 $(document).ready(function() {
 $().mousemove(function(e)
 {
 $('# MouseCoordinates ').html("X Axis Position = " + e.pageX + " and Y Axis Position = " + e.pageY);
 });
 

十一、獲取鼠標(biāo)指針的X / Y軸

 $().mousemove(function(e){
 //display the x and y axis values inside the P element
 $('p').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
 });

 十二、返回到頂部鏈接

此代碼對(duì)于比較長(zhǎng)的頁(yè)面非常實(shí)用,用戶(hù)不必拉滾動(dòng)條來(lái)返回頂部,直接點(diǎn)擊“返回頂部”即可。

 $(document).ready(function() {
 //when the id="top" link is clicked
 $('#top').click(function() {
 //scoll the page back to the top
 $(document).scrollTo(0,500);
 }
 });

jQuery是JavaScript最好的庫(kù)之一,支持Ajax及HTML 腳本客戶(hù)端,主要用于事件處理及制作動(dòng)畫(huà)。另外,jQuery還擁有各種插件,可以讓開(kāi)發(fā)者在最快的時(shí)間內(nèi)創(chuàng)建網(wǎng)頁(yè)。

希望本文所述對(duì)大家學(xué)習(xí)jquery程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 使用jQuery卸載全部事件的思路詳解

    使用jQuery卸載全部事件的思路詳解

    本文是小編給大家?guī)?lái)的jquery卸載全部事件的思路,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友一起看看吧!
    2017-04-04
  • jQuery的Ajax的自動(dòng)完成功能控件簡(jiǎn)要說(shuō)明

    jQuery的Ajax的自動(dòng)完成功能控件簡(jiǎn)要說(shuō)明

    jQuery的Ajax的自動(dòng)完成功能 允許您輕松地創(chuàng)建自動(dòng)完成/自動(dòng)提示框的文本輸入字段;如果沒(méi)有特定查詢(xún)的結(jié)果,它停止發(fā)送請(qǐng)求到服務(wù)器的其他查詢(xún)感興趣的朋友可以參考下啊
    2013-02-02
  • js jquery驗(yàn)證銀行卡號(hào)信息正則學(xué)習(xí)

    js jquery驗(yàn)證銀行卡號(hào)信息正則學(xué)習(xí)

    銀行卡號(hào)格式驗(yàn)證如何錯(cuò)誤將提示:格式錯(cuò)誤,應(yīng)該是19位數(shù)字,利用正則實(shí)現(xiàn),感興趣的朋友可以了解下,希望本文對(duì)你學(xué)習(xí)正則有所幫助
    2013-01-01
  • 詳解jquery事件delegate()的使用方法

    詳解jquery事件delegate()的使用方法

    這篇文章主要詳解了jquery事件delegate()的使用方法,delegate() 方法為指定的元素(屬于被選元素的子元素)添加一個(gè)或多個(gè)事件處理程序,并規(guī)定當(dāng)這些事件發(fā)生時(shí)運(yùn)行的函數(shù),感興趣的小伙伴們可以參考一下
    2016-01-01
  • jquery 動(dòng)態(tài)創(chuàng)建元素的方式介紹及應(yīng)用

    jquery 動(dòng)態(tài)創(chuàng)建元素的方式介紹及應(yīng)用

    動(dòng)態(tài)創(chuàng)建元素可以通過(guò)兩種方式1、Dom HTml2、JQuery函數(shù)創(chuàng)建3、頁(yè)面加載的時(shí)候最好在頁(yè)面加載完后執(zhí)行創(chuàng)建,感興趣的朋友可以了解下
    2013-04-04
  • 文本溢出插件jquery.dotdotdot.js使用方法詳解

    文本溢出插件jquery.dotdotdot.js使用方法詳解

    這篇文章主要介紹了文本溢出插件jquery.dotdotdot.js使用方法詳解,需要的朋友可以參考下
    2017-06-06
  • jquery中map函數(shù)與each函數(shù)的區(qū)別實(shí)例介紹

    jquery中map函數(shù)與each函數(shù)的區(qū)別實(shí)例介紹

    &#8203;jquery中的each函數(shù)和map函數(shù)的用法看起來(lái)差不多,但其實(shí)還是有一點(diǎn)區(qū)別的,each返回的是原來(lái)的數(shù)組,并不會(huì)新創(chuàng)建一個(gè)數(shù)組。而map方法會(huì)返回一個(gè)新的數(shù)組
    2014-06-06
  • 讀jQuery之二(兩種擴(kuò)展)

    讀jQuery之二(兩種擴(kuò)展)

    上一篇分析了jQuery對(duì)象的組成,這篇分析下它的extend方法。
    2011-06-06
  • jquery星級(jí)插件、支持頁(yè)面中多次使用

    jquery星級(jí)插件、支持頁(yè)面中多次使用

    一個(gè)關(guān)于jquery星級(jí)插件的博文,那是我從網(wǎng)上收集的,它只支持一個(gè)頁(yè)面中使用一次,多次使用的話(huà)會(huì)發(fā)生沖突,達(dá)不到我項(xiàng)目的需求,沒(méi)辦法,只能修改它
    2012-03-03
  • jQuery實(shí)現(xiàn)的記住帳號(hào)密碼功能完整示例

    jQuery實(shí)現(xiàn)的記住帳號(hào)密碼功能完整示例

    這篇文章主要介紹了jQuery實(shí)現(xiàn)的記住帳號(hào)密碼功能,結(jié)合完整實(shí)例形式分析了jQuery使用jquery.cookie.js插件記錄用戶(hù)信息相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08

最新評(píng)論