国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

7個有用的jQuery代碼片段分享

2024-05-06 16:20:36
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了7個有用的jQuery技巧分享,本文給出了在新窗口打開鏈接、設置等高的列、jQuery預加載圖像、禁用鼠標右鍵、設定計時器等實用代碼片段,需要的朋友可以參考下

jQuery是一款輕量級的JavaScript庫,是最流行的客戶端HTML腳本之一,它在WEB設計師和開發者中非常的有名,并且有非常多有用的插件和技術幫助WEB開發人員開發出有創意和漂亮的WEB頁面。

今天我們為jQuery用戶分享一些小技巧,這些技巧將幫助你提示你網站布局和應用的創意性和功能性。

一、在新窗口打開鏈接

用下面的代碼,你點擊鏈接即可在新窗口打開:

 

 
  1. $(document).ready(function() { 
  2. //select all anchor tags that have http in the href 
  3. //and apply the target=_blank 
  4. $("a[href^='http']").attr('target','_blank'); 
  5. }); 

二、設置等高的列

應用下面的代碼,可以使得你的WEB應用每一列高度都想等:

 

 
  1. <div class="equalHeight" style="border:1px solid"
  2. <p>First Line</p> 
  3. <p>Second Line</p> 
  4. <p>Third Line</p> 
  5. </div> 
  6. <div class="equalHeight" style="border:1px solid"
  7. <p>Column Two</p> 
  8. </div> 
  9. <script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> 
  10. <script> 
  11. $(document).ready(function() { 
  12. equalHeight('.equalHeight'); 
  13. }); 
  14. //global variable, this will store the highest height value 
  15. var maxHeight = 0; 
  16. function equalHeight(col) { 
  17. //Get all the element with class = col 
  18. col = $(col); 
  19. //Loop all the col 
  20. col.each(function() { 
  21. //Store the highest value 
  22. if ($(this).height() > maxHeight) { 
  23. maxHeight = $(this).height(); 
  24. }); 
  25. //Set the height 
  26. col.height(maxHeight); 
  27. </script> 

三、jQuery預加載圖像

這個小技巧可以提升頁面加載圖片的速度:

 

 
  1. jQuery.preloadImagesInWebPage = function() { 
  2. for (var ctr = 0; ctr & lt; arguments.length; ctr++) { 
  3. jQuery("").attr("src", arguments[ctr]); 
  4. // 使用方法: 
  5. $.preloadImages("image1.gif""image2.gif""image3.gif"); 
  6. // 檢查圖片是否被加載 
  7. $('#imageObject').attr('src''image1.gif').load(function() { 
  8. alert('The image has been loaded…'); 
  9. }); 

四、禁用鼠標右鍵

 

 
  1. $(document).ready(function() { 
  2. //catch the right-click context menu 
  3. $(document).bind("contextmenu"function(e) { 
  4. //warning prompt - optional 
  5. alert("No right-clicking!"); 
  6. //delete the default context menu 
  7. return false
  8. }); 
  9. }); 

五、設定計時器

 

 
  1. $(document).ready(function() { 
  2. window.setTimeout(function() { 
  3. // some code 
  4. }, 500); 
  5. }); 

六、計算子元素的個數

 

 
  1. <div id="foo"
  2. <div id="bar"></div> 
  3. <div id="baz"
  4. <div id="biz"></div> 
  5. <span><span></span></span> 
  6. </div> 
  7. </div> 
  8. <script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> 
  9. <script type="text/javascript"
  10. //jQuery code to count child elements $("#foo > div").size() 
  11. alert($("#foo > div").size()) 
  12. </script> 

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

 

 
  1. <div id="foo" style="width:200px;height: 200px;background: #ccc;"></div> 
  2. <script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> 
  3. <script type="text/javascript"
  4. jQuery.fn.center = function() { 
  5. this.css("position""absolute"); 
  6. this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px"); 
  7. this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px"); 
  8. return this
  9. //Use the above function as: 
  10. $('#foo').center(); 
  11. </script> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 旌德县| 保山市| 堆龙德庆县| 高尔夫| 吉首市| 静安区| 白沙| 邛崃市| 大竹县| 富平县| 罗源县| 墨脱县| 英超| 黄大仙区| 东至县| 万全县| 惠安县| 即墨市| 吉林省| 大理市| 桃江县| 谷城县| 长治县| 隆安县| 航空| 满洲里市| 肇庆市| 额济纳旗| 丰城市| 陇南市| 南康市| 汉川市| 张家港市| 河源市| 九寨沟县| 昌邑市| 疏附县| 杨浦区| 镇江市| 垣曲县| 商南县|