備注:待驗證。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、以下待驗證、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
今天度娘發現這個內容,原來我一直做的都能直接有函數方法,汗死姐了。
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); });
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
jQuery.URLParams()
這個函數我起碼寫過100遍。
http://www.domain.com/this/domain/rocks?param=fantastic&test=awesome#websanova $.URLParams(); // {param: 'fantastic', test: 'awesome'} $.URLParams('test'); // awesome
jQuery.URLHash()
這個函數類似 URLParams,但是它返回的是 URL 中的 “#” 部分,如果有的話。
http://www.domain.com/this/domain/rocks?param=fantastic&test=awesome#websanova $.URLHash(); // websanova
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
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"
.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
.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