首頁 > web前端 > js教程 > 主體

jQuery資料型態小結(14個)_jquery

WBOY
發布: 2016-05-16 15:21:12
原創
1119 人瀏覽過

jQuery除了包含原生JS中的內建資料類型(built-in datatype),還包括一些擴充的資料類型(virtual types),如Selectors、Events等。

1. String

String最常見,幾乎任何一門高級程式語言和腳本語言中都支持,例如"Hello world!"即字串。字串的類型為string。如

var typeOfStr = typeof "hello world";//typeOfStr為「string"

1.1 String內建方法

"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屬性:傳回字元長度,例如"hello".length回傳5

1.3 字串轉換為Boolean:

一個空字串("")預設為false,而一個非空字串為true(例如"hello")。

2. Number

數字類型,如3.1415926或1、2、3...

typeof 3.1415926 回傳的是"number"

2.1 Number轉換為Boolean:

如果一個Number值為0,則預設為false,否則為true。

2.2 由於Number是採用雙精確度浮點數實現的,所以下面這種情況是合理的:

0.1 + 0.2 // 0.30000000000000004

3. Math

下面的方法與Java中的Math類別的靜態方法類似。

Math.PI // 3.141592653589793
Math.cos(Math.PI) // -1

3.1 將字串化為數字:parseInt和parseFloat方法:

parseInt("123") = 123 (採用十進位轉換)
parseInt("010") = 8 (採用八進位轉換)
parseInt("0xCAFE") = 51966 (採用十六進位轉換)
parseInt("010", 10) = 10 (指定用10進位轉換)
parseInt("11", 2) = 3 (指定用二進位轉換)
parseFloat("10.10") = 10.1

3.2 數字到字串

當將Number黏在(append)字串後的時候,將會得到字串。
"" + 1 + 2; // "12"
"" + (1 + 2); // "3"
"" + 0.0000001; // "1e-7"
或用強制型別轉換:
String(1) + String(2); //"12"
String(1 + 2); //"3"

4. NaN 和 Infinity

如果對一個非數字字串呼叫parseInt方法,將傳回NaN(Not a Number),NaN常用來偵測一個變數是否數字型,如下:

isNaN(parseInt("hello", 10)) // true
Infinity表示數值無窮大或無窮小,例如1 / 0 // Infinity。

對NaN和Infinity呼叫typeof運算子都會回傳"numuber"。

另外 NaN==NaN 回傳false,但是 Infinity==Infinity 回傳true。

5. Integer 和 Float

分為表示整數和浮點型。

6. BOOLEAN

布林類型,true或false。

7. OBJECT

JavaScript中的一切皆物件。對一個物件進行typeof運算傳回 "object"。

var x = {}; 
var y = { name: "Pete", age: 15 };
登入後複製

對於上面的y對象,可以採用圓點取得屬性值,例如y.name回傳"Pete",y.age回傳15

7.1 Array Notation(陣列存取方式存取物件)

var operations = { increase: "++", decrease: "--" } 
var operation = "increase"; 
operations[operation] // "++"; 
operations["multiply"] = "*"; // "*"
登入後複製

上面operations["multiply"]="*"; 往operations物件中新增了一個key-value對。

7.2 物件循環存取:for-in

var obj = { name: "Pete", age: 15}; 
for(key in obj) { 
alert("key is "+[key]+", value is "+obj[key]); 
}
登入後複製

7.3 任何物件不管有無屬性和值,都預設為true

7.4 物件的Prototype屬性

jQuery中以fn(Prototype的別名)動態為jQuery Instances加入物件(函數)

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外掛都提供了一個基於OPTIONS的API,OPTIONS是JS對象,表示該物件以及它的屬性都是optional(可選的)。允許customization。
例如採用Ajax方式提交表單,

$("#myform").ajaxForm();//預設採用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也是物件。

讀取或設定ARRAY中元素的值,採用這種方式:

var val = arr[0];//val为1
arr[2] = 4;//现在arr第三个元素为4
登入後複製

9.1 陣列循環(遍歷)

for (var i = 0; i < a.length; i++) { // Do something with a[i] }
但是当考虑性能时,则最好只读一次length属性,如下:
for (var i = 0, j = a.length; i < j; i++) { // Do something with a[i] }
jQuery提供了each方法遍历数组:
var x = [1, 2, 3]; 
$.each(x, 
function(index, value) { 
console.log("index", index, "value", value); 
});
登入後複製

9.2 對數組呼叫push方法意味著將一個元素加到數組末尾,例如 x.push(5); 和 x.[x.length] = 5; 等價

9.3 陣列其他內建方法:

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) // 用于插入、删除或替换数组元素,这里为删除从index=1开始的2个元素
登入後複製

9.4 数组为对象,所以始终为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标准事件包括: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对象包含DOM元素的集合。比如$('p')即返回所有

...


JQUERY对象行为类似数组,也有length属性,也可以通过index访问DOM元素集合中的某个。但是不是数组,不具备数组的某些方法,比如join()。

许多jQuery方法返回jQuery对象本身,所以可以采用链式调用:
$("p").css("color", "red").find(".special").css("color", "green");
但是如果你调用的方法会破坏jQuery对象,比如find()和filter(),则返回的不是原对象。要返回到原对象只需要再调用end()方法即可。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!