jQuery除了包含原生JS中的內(nèi)置數(shù)據(jù)類型(built-in datatype),,還包括一些擴(kuò)展的數(shù)據(jù)類型(virtual types),如Selectors,、Events等,。
1. String String最常見,幾乎任何一門高級(jí)編程語(yǔ)言和腳本語(yǔ)言中都支持,,比如"Hello world!"即字符串,。字符串的類型為string。比如 var typeOfStr = typeof "hello world";//typeOfStr為“string" 1.1 String內(nèi)置方法 "hello".charAt(0) // "h" "hello".toUpperCase() // "HELLO" "Hello".toLowerCase() // "hello" "hello".replace(/e|o/g, "x") // "hxllx" "1,2,3".split(",") // ["1", "2", "3"] 1.2 length屬性:返回字符長(zhǎng)度,,比如"hello".length返回5 1.3 字符串轉(zhuǎn)換為Boolean: 一個(gè)空字符串("")默認(rèn)為false,,而一個(gè)非空字符串為true(比如"hello")。
2. Number 數(shù)字類型,,比如3.1415926或者1,、2、3... typeof 3.1415926 返回的是"number" 2.1 Number轉(zhuǎn)換為Boolean: 如果一個(gè)Number值為0,,則默認(rèn)為false,,否則為true。 2.2 由于Number是采用雙精度浮點(diǎn)數(shù)實(shí)現(xiàn)的,,所以下面這種情況是合理的: 0.1 + 0.2 // 0.30000000000000004
3. Math 下面的方法與Java中的Math類的靜態(tài)方法類似,。 Math.PI // 3.141592653589793 Math.cos(Math.PI) // -1 3.1 將字符串化為數(shù)字:parseInt和parseFloat方法: parseInt("123") = 123 (采用十進(jìn)制轉(zhuǎn)換) parseInt("010") = 8 (采用八進(jìn)制轉(zhuǎn)換) parseInt("0xCAFE") = 51966 (采用十六進(jìn)制轉(zhuǎn)換) parseInt("010", 10) = 10 (指定用10進(jìn)制轉(zhuǎn)換) parseInt("11", 2) = 3 (指定用二進(jìn)制轉(zhuǎn)換) parseFloat("10.10") = 10.1 3.2 數(shù)字到字符串 當(dāng)將Number粘到(append)字符串后的時(shí)候,將得到字符串,。 "" + 1 + 2; // "12" "" + (1 + 2); // "3" "" + 0.0000001; // "1e-7" 或者用強(qiáng)制類型轉(zhuǎn)換: String(1) + String(2); //"12" String(1 + 2); //"3"
4. NaN 和 Infinity 如果對(duì)一個(gè)非數(shù)字字符串調(diào)用parseInt方法,,將返回NaN(Not a Number),NaN常用來(lái)檢測(cè)一個(gè)變量是否數(shù)字類型,,如下: isNaN(parseInt("hello", 10)) // true Infinity表示數(shù)值無(wú)窮大或無(wú)窮小,,比如1 / 0 // Infinity。 對(duì)NaN和Infinity調(diào)用typeof運(yùn)算符都返回"numuber",。 另外 NaN==NaN 返回false,,但是 Infinity==Infinity 返回true。
5. Integer 和 Float 分為表示整型和浮點(diǎn)型,。
6. BOOLEAN 布爾類型,,true或者false。
7. OBJECT JavaScript中的一切皆對(duì)象,。對(duì)一個(gè)對(duì)象進(jìn)行typeof運(yùn)算返回 "object",。 var x = {}; var y = { name: "Pete", age: 15 }; 對(duì)于上面的y對(duì)象,可以采用圓點(diǎn)獲取屬性值,,比如y.name返回"Pete",,y.age返回15 7.1 Array Notation(數(shù)組訪問方式訪問對(duì)象) var operations = { increase: "++", decrease: "--" } var operation = "increase"; operations[operation] // "++"; operations["multiply"] = "*"; // "*" 上面operations["multiply"]="*"; 往operations對(duì)象中添加了一個(gè)key-value對(duì)。 7.2 對(duì)象循環(huán)訪問:for-in var obj = { name: "Pete", age: 15}; for(key in obj) { alert("key is "+[key]+", value is "+obj[key]); } 7.3 任何對(duì)象不管有無(wú)屬性和值,,都默認(rèn)為true 7.4 對(duì)象的Prototype屬性 jQuery中用fn(Prototype的別名)動(dòng)態(tài)為jQuery Instances添加對(duì)象(函數(shù)) var form = $("#myform"); form.clearForm; // undefined form.fn.clearForm = function() { return this.find(":input").each(function() { this.value = ""; }).end(); }; form.clearForm() // works for all instances of jQuery objects, because the new method was added
8. OPTIONS 幾乎所有的jQuery插件都提供了一個(gè)基于OPTIONS的API,,OPTIONS是JS對(duì)象,意味著該對(duì)象以及它的屬性都是optional(可選的),。允許customization,。 比如采用Ajax方式提交表單, $("#myform").ajaxForm();//默認(rèn)采用Form的Action屬性值作為Ajax-URL,,Method值作為提交類型(GET/POST) $("#myform").ajaxForm({ url: "mypage.php", type: "POST" });//則覆蓋了提交到的URL和提交類型
9. ARRAY var arr = [1, 2, 3]; ARRAY是可變的lists,。ARRAY也是對(duì)象。 讀取或設(shè)置ARRAY中元素的值,,采用這種方式: var val = arr[0];//val為1 arr[2] = 4;//現(xiàn)在arr第三個(gè)元素為4 9.1 數(shù)組循環(huán)(遍歷) for (var i = 0; i < a.length; i++) { // Do something with a[i] } 但是當(dāng)考慮性能時(shí),,則最好只讀一次length屬性,如下: for (var i = 0, j = a.length; i < j; i++) { // Do something with a[i] } jQuery提供了each方法遍歷數(shù)組: var x = [1, 2, 3]; $.each(x, function(index, value) { console.log("index", index, "value", value); }); 9.2 對(duì)數(shù)組調(diào)用push方法意味著將一個(gè)元素添加到數(shù)組末尾,,比如 x.push(5); 和 x.[x.length] = 5; 等價(jià) 9.3 數(shù)組其他內(nèi)置方法: var x = [0, 3, 1, 2]; x.reverse() // [2, 1, 3, 0] x.join(" – ") // "2 - 1 - 3 - 0" x.pop() // [2, 1, 3] x.unshift(-1) // [-1, 2, 1, 3] x.shift() // [2, 1, 3] x.sort() // [1, 2, 3] x.splice(1, 2) // 用于插入,、刪除或替換數(shù)組元素,這里為刪除從index=1開始的2個(gè)元素 9.4 數(shù)組為對(duì)象,,所以始終為true
10. MAP The map type is used by the AJAX function to hold the data of a request. This type could be a string, an array<form elements>, a jQuery object with form elements or an object with key/value pairs. In the last case, it is possible to assign multiple values to one key by assigning an array. As below: {'key[]':['valuea','valueb']}
11. FUNCTION:匿名和有名兩種 11.1 Context,、Call和Apply In JavaScript, the variable "this" always refers to the current context. $(document).ready(function() { // this refers to window.document}); $("a").click(function() { // this refers to an anchor DOM element });
12. SELECTOR There are lot of plugins that leverage jQuery's selectors in other ways. The validation plugin accepts a selector to specify a dependency, whether an input is required or not: emailrules: { required: "#email:filled" } This would make a checkbox with name "emailrules" required only if the user entered an email address in the email field, selected via its id, filtered via a custom selector ":filled" that the validation plugin provides.
13. EVENT DOM標(biāo)準(zhǔn)事件包括:blur, focus, load, resize, scroll, unload, beforeunload, click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, andkeyup
14. JQUERY JQUERY對(duì)象包含DOM元素的集合。比如$('p')即返回所有<p>...</p> JQUERY對(duì)象行為類似數(shù)組,,也有l(wèi)ength屬性,,也可以通過index訪問DOM元素集合中的某個(gè)。但是不是數(shù)組,,不具備數(shù)組的某些方法,,比如join()。 許多jQuery方法返回jQuery對(duì)象本身,,所以可以采用鏈?zhǔn)秸{(diào)用: $("p").css("color", "red").find(".special").css("color", "green"); 但是如果你調(diào)用的方法會(huì)破壞jQuery對(duì)象,,比如find()和filter(),則返回的不是原對(duì)象,。要返回到原對(duì)象只需要再調(diào)用end()方法即可,。 |
|
來(lái)自: 我的前端圖書館 > 《jquery數(shù)據(jù)類型及處理》