jQuery實用技巧必備(下)
本文實例總結(jié)了經(jīng)典且實用的jQuery代碼開發(fā)技巧。分享給大家供大家參考。具體如下:
23. jQuery延時加載功能
Want to delay something?
$(document).ready(function() { window.setTimeout(function() { // do something }, 1000); });
24. 移除單詞功能
Want to remove a certain word(s)?
$(document).ready(function() { var el = $('#id'); el.html(el.html().replace(/word/ig, "")); });
25. 驗證元素是否存在于jquery對象集合中
Simply test with the .length property if the element exists.
$(document).ready(function() { if ($('#id').length) { // do something } });
26. 使整個DIV可點擊
Want to make the complete div clickable?
$(document).ready(function() { $("div").click(function(){ //get the url from href attribute and launch the url window.location=$(this).find("a").attr("href"); return false; }); // how to use <DIV><A href="index.html">home</A></DIV> });
27. ID與Class之間轉(zhuǎn)換
當(dāng)改變Window大小時,在ID與Class之間切換
$(document).ready(function() { function checkWindowSize() { if ( $(window).width() > 1200 ) { $('body').addClass('large'); } else { $('body').removeClass('large'); } } $(window).resize(checkWindowSize); });
28. 克隆對象
Clone a div or an other element.
$(document).ready(function() { var cloned = $('#id').clone(); // how to use <DIV id=id></DIV> });
29. 使元素居屏幕中間位置
Center an element in the center of your screen.
$(document).ready(function() { 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; } $("#id").center(); });
30. 寫自己的選擇器
Write your own selectors.
$(document).ready(function() { $.extend($.expr[':'], { moreThen1000px: function(a) { return $(a).width() > 1000; } }); $('.box:moreThen1000px').click(function() { // creating a simple js alert box alert('The element that you have clicked is over 1000 pixels wide'); }); });
31. 統(tǒng)計元素個數(shù)
Count an element.
$(document).ready(function() { $("p").size(); });
32. 使用自己的Bullets
Want to use your own bullets instead of using the standard or images bullets?
$(document).ready(function() { $("ul").addClass("Replaced"); $("ul > li").prepend("‒ "); // how to use ul.Replaced { list-style : none; } });
33. 引用Google主機(jī)上的Jquery類庫
Let Google host the jQuery script for you. This can be done in 2 ways.
//Example 1 <SCRIPT src="http://www.google.com/jsapi"></SCRIPT> <SCRIPT type=text/javascript> google.load("jquery", "1.2.6"); google.setOnLoadCallback(function() { // do something }); </SCRIPT><SCRIPT type=text/javascript src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT> // Example 2:(the best and fastest way) <SCRIPT type=text/javascript src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT>
34. 禁用Jquery(動畫)效果
Disable all jQuery effects
$(document).ready(function() { jQuery.fx.off = true; });
35. 與其他Javascript類庫沖突解決方案
To avoid conflict other libraries on your website, you can use this jQuery Method, and assign a different variable name instead of the dollar sign.
$(document).ready(function() { var $jq = jQuery.noConflict(); $jq('#id').show(); });
以上就是所有關(guān)于jQuery實用技巧的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- jQuery基礎(chǔ)學(xué)習(xí)技巧總結(jié)
- jQuery實用技巧
- jQuery 技巧大全(新手入門篇)
- jQuery 常見開發(fā)使用技巧總結(jié)
- Jquery下的26個實用小技巧(jQuery tips, tricks & solutions)
- jQuery編寫widget的一些技巧分享
- Jquery常用技巧收集整理篇
- jquery常用技巧及常用方法列表集合
- 關(guān)于jQuery UI 使用心得及技巧
- 關(guān)于jQuery新的事件綁定機(jī)制on()的使用技巧
- jquery操作復(fù)選框(checkbox)的12個小技巧總結(jié)
- jQuery操作表格(table)的常用方法、技巧匯總
- 編寫高效jQuery代碼的4個原則和5個技巧
- jQuery源碼分析之jQuery中的循環(huán)技巧詳解
- 分享五個有用的jquery小技巧
- jQuery實用技巧必備(上)
- jQuery實用技巧必備(中)
- jQuery實用技巧必備(下)
相關(guān)文章
jQuery dateRangePicker插件使用方法詳解
這篇文章主要為大家詳細(xì)介紹了jQuery dateRangePicker插件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07jquery取子節(jié)點及當(dāng)前節(jié)點屬性值的方法
這篇文章主要介紹了jquery取子節(jié)點及當(dāng)前節(jié)點屬性值的方法,比較實用,需要的朋友可以參考下2014-09-09PageSwitch插件實現(xiàn)100種不同圖片切換效果
這篇文章主要介紹了PageSwitch插件實現(xiàn)100種不同圖片切換效果,需要的朋友可以參考下2015-07-07jquery 圖片Silhouette Fadeins漸顯效果
經(jīng)常漂流在css-tricks看到這篇文章,就順便搬了過來,下面譯文都是用css-tricks口吻來描述的。2010-02-02bootstrap table實現(xiàn)iview固定列的效果實例代碼詳解
這篇文章主要介紹了bootstrap table實現(xiàn)iview固定列的效果,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09jQuery 翻牌或百葉窗效果(內(nèi)容三秒自動切換)
jQuery 翻牌或百葉窗效果(內(nèi)容三秒自動切換),讓列表內(nèi)容不再枯燥2012-06-06幾種二級聯(lián)動案例(jQuery\Array\Ajax php)
這篇文章主要為大家詳細(xì)介紹了幾種二級聯(lián)動案例(jQuery\Array\Ajax php),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08