分享12個實用的jQuery程式碼片段,12jquery程式碼片段

來源:互聯網
上載者:User

分享12個實用的jQuery程式碼片段,12jquery程式碼片段

jQuery是一款優秀的JavaScript庫,它在WEB開發人員和網頁設計師中非常出名,協助網頁設計師開發出很多有創意和漂亮的WEB頁面。

本文主要分享了12個有用的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');}); 

二、設定計時器

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

 

三、設定等高的列

使用下面的代碼,可以使得你的網頁應用每一列高度都一樣:

<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>

 四、jQuery預先載入映像

這個技巧可以加快網頁載入圖片的速度:

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…');});

 五、把元素定位到頁面中間

<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> 

六、禁用滑鼠右鍵

$(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; });});

 七、計運算元元素的個數

<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設定文字大小

加入這段代碼,使用者可根據需求重新設定文本尺寸(增加或減少)。

 $(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); }); });

十、檢測當前滑鼠座標

使用這個JS代碼,你可以在任何瀏覽器裡擷取滑鼠座標。

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

十一、擷取滑鼠指標的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); });

 十二、返回到頂部連結

此代碼對於比較長的頁面非常實用,使用者不必拉捲軸來返回頂部,直接點擊“返回頂部”即可。

 $(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最好的庫之一,支援Ajax及HTML 指令碼用戶端,主要用於事件處理及製作動畫。另外,jQuery還擁有各種外掛程式,可以讓開發人員在最快的時間內建立網頁。

希望本文所述對大家學習jquery程式設計有所協助。

您可能感興趣的文章:
  • JavaScript和JQuery實用程式碼片段(一)
  • jquery實用程式碼片段集合
  • 一些實用的jQuery程式碼片段收集
  • 7個有用的jQuery程式碼片段分享
  • web前端開發JQuery常用執行個體程式碼片段(50個)
  • 直接拿來用的15個jQuery程式碼片段
  • 10個很棒的jQuery程式碼片段
  • 18個非常棒的jQuery程式碼片段
  • 程式員必知35個jQuery 程式碼片段
  • 15個常用的jquery程式碼片段

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.