jQuery 工具大搜集

openkk 12年前發布 | 23K 次閱讀 jQuery Ajax框架

jQuery 是一個非常棒的類庫,但是為了保證代碼的干凈以及代碼的精簡,它只提供最核心的功能。所以就有了很多其他的工具來豐富jQuery的功能。我在使用這些工具的時候發現我常常重復的編寫一些代碼,所以我就開始把它們整理到一個類庫中。我把這些代碼都包裝成了jQuery的代碼,但這并不是必須的,你也可以在其他JS類庫中使用他們,或者單獨使用。

你可以在 這里下載這個類庫。

jQuery.postJSON()
我不明白為何 jQuery 沒有把這個方法加進去,雖然他們有 $.getJSON 方法。

$.postJSON(
    "/put/path/here",
    {val1: "Cheetos", val2: "Nachos"},
    function(response){ //on success do something }
);
jQuery.stop()
這個方法是用來停止事件傳遞的。它接受兩個參數,preventDefault 和 stopPropgation。

$.stop(event, preventDefault, stopPropagation);

$("#container").click(function(e) { $.stop(e, true, true);
});</pre>jQuery.shuffleArray()
這個我用的不多,但是很好用,它可以隨機的打亂一個數組。

$.shuffleArray([1,2,3,4,5,6,7]); //potential output: [1,3,5,7,2,4,6]
jQuery.reload()
這個函數就是“window.location.reload(true)”的縮寫。

$.reload();
jQuery.uri()
這個函數可以解析 URL 的 URI 部分,可以通過 index 的方式訪問,從1開始。

http://www.domain.com/this/domain/rocks

$.uri(1); //will output this $.uri(3); //will output rocks</pre>jQuery.URLParams()
這個函數我起碼寫過100遍。

http://www.domain.com/this/domain/rocks?param=fantastic&test=awesome#websanova

$.URLParams(); // {param: 'fantastic', test: 'awesome'} $.URLParams('test'); // awesome</pre>jQuery.URLHash()
這個函數類似 URLParams,但是它返回的是 URL 中的 “#” 部分,如果有的話。

http://www.domain.com/this/domain/rocks?param=fantastic&test=awesome#websanova

$.URLHash(); // websanova</pre>jQuery.hexToRGB()
這個函數也很好用,它接受一個十六進制的數字,或者RGB字符串,相互轉換,非法數據會返回 false。

$.hexToRGB("#FF3388"); // rgb(255,51,136)
$.hexToRGB("#F38");    // rgb(255,51,136)
$.hexToRGB("#ZZ3388"); // false
$.hexToRGB("F38A");    // false

$.hexToRGB("rgb(22,67,234)"); // #1643EA $.hexToRGB("rgb(22,67,274)"); // false $.hexToRGB("rgb(22,67)"); // false</pre>jQuery.base64Encode()
將 UTF-8 的字符串用 base64 編碼。

$.base64Encode("encode this string"); // ZW5jb2RlIHRoaXMgc3RyaW5n
jQuery.base64Decode()
base64 解碼,同樣也是 UTF-8 編碼。

$.base64Decode("ZW5jb2RlIHRoaXMgc3RyaW5n"); // encode this string
jQuery.utf8Encode()
將 String 轉換成 UTF-8 編碼的,主要用于上面提到的 base64Encode 函數。

$.utf8Encode("utf8 encode this");
jQuery.utf8Decode()
UTF-8 解碼。

$.utf8Decode("utf8 encode this");
.removeClassRegEx()
這個方法真是太好用了,可以移除指定元素的指定 class。

<div class="test testing leavemealone hellotest Tester"></div>

$("#container").removeClassRegEx(/test/i); //class="leavemealone" $("#container").removeClassRegEx(/test/); //class="leavemealone Tester" $("#container").removeClassRegEx(/^test/i); //class="leavemealone hellotest" $("#container").removeClassRegEx(/test$/); //class="testing leavemealone Tester"</pre>.hasClassRegEx()
和 removeClassRegEx 類似,這個方法檢查指定元素是否有指定的 class。

<div class="test testing leavemealone hellotest Tester"></div>

$("#container").removeClassRegEx(/test/i); // true $("#container").removeClassRegEx(/test/); // true $("#container").removeClassRegEx(/^test/i); // true $("#container").removeClassRegEx(/test$/); // true $("#container").removeClassRegEx(/^testy$/); // false</pre>.maxChars()
這個函數對那些沒有“maxlength”屬性的input元素就很有用。它也可以指定一個元素來顯示剩余字符。

$("input").maxChars(50);
$("input").maxChars(50, $("#maxChars_counter"));
Object.sizeof()
這個方法是 JavaScript 的一個擴展,可以讓你獲取對象的長度。

{cow: "moo", duck: "quack"}.sizeof(); // 2
String.capitalize()
這是 String 對象的一個擴展,可以把一個字符串變成大寫的。

"test".capitalize(); // Test
String.pxToInt()
這個方法用了很多,特別是在我返回一個 CSS 屬性的時候,我希望得到一個整數。

"210px".pxToInt(); //210
$("container").css('height').pxToInt(); // 400

原文鏈接, OSChina.NET 原創編譯

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!