1. $ 是 JQuery 的 Alias
2. boolean: true/false
3. 宣告物件
var emp = {
name: "Zara",
age: 10
};
引用 emp.name // 結果: Zara
4. Array 宣告
var x = [];
var y = [1, 2, 3, 4, 5];
y.length // Array y 的長度 ==> 5
for (var i = 0; i < y.length; i++) {
// Do something with y
}
5. named function
function named(){
// do some stuff here
}
6. anonymous function (未命名函式)
var handler = function (){
// do some stuff here
}
$(document).ready(function(){
// do some stuff here
});
可簡化成
$(function(){
// do some stuff here
});
7. 引數 Argument, 函式的參數皆可用 arguments,length 取得參數長度, arguments.callee 取得函式的名稱
function func(x){
console.log(typeof x, arguments.length);
}
func(); //==> "undefined", 0
func(1); //==> "number", 1
func("1", "2", "3"); //==> "string", 3
8. context (this)
$(document).ready(function() {
// this refers to window.document
});
$("div").click(function() {
// this refers to a div DOM element
});
9. Callback
$("body").click(function(event) {
console.log("clicked: " + event.target);
});
$("#myform").submit(function() {
return false;
});
10. Built-in Functions
charAt(), concat(), forEach(),
indexOf() : -1 not found
string => length(), substr(), toLowCase(), toUpperCase(),
Array => pop(), push(), reverse(), sort(),
toString() // get number to string
11. SELECTORS
Tag Name ==> $('p') 選取所有 Tag <p>
Tag ID ==> $('#someid') 選取 Tag ID someid
Tag Class ==> $('.some-class') 選取所有elements 含指定 class 名稱者