文章目錄
jQuery對象
jQuery對象是一個僞數組
什麼是僞數組:有數組的屬性,即 0–(length-1) 的屬性並且有 length 屬性
<body>
<div>div1</div>
<div>div2</div>
<div>div3</div>
</body>
<script src="../jquery.js"></script>
<script>
$(function(){
var $div = $("div");
console.log($div);
})
</script>
靜態方法和實例方法
先來回顧一下什麼是靜態方法、什麼是實例方法
靜態方法: 直接添加到類上面的,通過類名調用
實例方法: 添加到實例原型上面的,通過實例調用
註釋部分爲特別關注!!!
<body>
<div>div1</div>
<div>div2</div>
<div>div3</div>
</body>
<script src="../jquery.js"></script>
<script>
//定義一個類
function Person() {
}
//給這個類添加一個靜態方法,直接添加給類的就是靜態方法
Person.staticMethod = function() {
alert("staticMethod");
}
//靜態方法通過類名調用
Person.staticMethod();
//給這個類添加一個實例方法
Person.prototype.instanceMethod = function() {
alert("instanceMethod");
};
//實例方法通過類的實例調用,首先創建一個實例(創建一個對象)
var person = new Person();
person.instanceMethod();
</script>
jQuery的each方法與原生JS的forEach()比對
注意:
(1)原生JS的forEach() 可以傳遞兩個參數。第一個參數是遍歷的當前元素,第二個參數是當前遍歷元素的索引值
(2)jQuery的each方法 可以傳遞兩個參數。第一個參數是當前遍歷元素的索引值,第二個參數是遍歷的當前元素。正好與原生JS的forEach()相反
(3)原生JS的forEach()只能遍歷數組,不能遍歷僞數組。jQuery的each方法既能遍歷數組又可以遍歷僞數組的
註釋部分爲特別關注!!!
<script>
var arr = [1,2,3,4,5];
//僞數組
var obj = {0:7, 1:8, 2:9,length:3};
//第一個參數:遍歷的當前元素
//第二個參數:當前遍歷元素的索引值
//注意:原生的forEach()只能遍歷數組,不能遍歷僞數組
arr.forEach(function(value,index){
console.log(index,value);
})
//利用jQuery的each靜態方法遍歷數組
//第一個參數:當前遍歷元素的索引值
//第二個參數:遍歷的當前元素
//注意:jQuery的each方法是可以遍歷僞數組的
$.each(arr,function(index,value){
console.log(index,value);
});
$.each(obj,function(index,value){
console.log(index,value);
})
</script>
jQuery的map方法與原生JS的map方法比對
注意:
(1)原生JS的map方法可以傳遞三個個參數。第一個參數是當前所遍歷的元素,第二個參數是當前遍歷元素的索引,第三個參數是當前被遍歷的數組。
(2)jQuery的map方法傳遞兩個參數。第一個參數是要遍歷的數組,第二個參數是每遍歷一個元素之後執行的回調函數。回調函數中又可以傳遞兩個參數,第一個參數是當前遍歷到的元素,第二個參數是所遍歷元素的索引。
(3)原生JS的map方法不能遍歷僞數組,jQuery的map方法可以遍歷僞數組
註釋部分爲特別關注!!!
<script src="../jquery.js"></script>
<script>
var arr = [1,2,3,4,5];
var obj = {0:7, 1:8, 2:9,length:3};
//利用原生JS的map方法遍歷
//第一個參數:當前所遍歷的元素
// 第二個參數:當前遍歷元素的索引
//第三個參數:當前被遍歷的數組
//注意:不能遍歷僞數組
arr.map(function(value,index,array){
console.log(index,value,array);
})
console.log("我是分割線");
//jQuery的map方法
//第一個參數:要遍歷的數組
//第二個參數:每遍歷一個元素之後執行的回調函數
//回調函數中的參數有兩個
//第一個參數:當前遍歷到的元素
//第二個參數:所遍歷元素的索引
//注意:可以遍歷僞數組
$.map(arr,function(value,index){
console.log(index,value);
})
</script>
jQuery的each方法與jQuery的map方法的不同之處
區別一
(1)each靜態方法默認的返回值是它所遍歷的數組,遍歷誰就返回誰
(2)map靜態方法默認的返回值是一個空數組
註釋部分爲特別關注!!!
<script src="../jquery.js"></script>
<script>
var arr = [1,2,3,4,5];
var obj = {0:7, 1:8, 2:9,length:3};
//map和each的區別1
//each靜態方法默認的返回值是它所遍歷的數組,遍歷誰就返回誰
//map靜態方法默認的返回值是一個空數組
var a = $.map(obj,function(value,index){
console.log(index,value);
})
var b = $.each(obj,function(index,value){
console.log(index,value);
})
console.log(a);
console.log(b);
</script>
區別二
(1)each靜態方法不支持在回調函數中對遍歷的數組進行處理
(2)map靜態方法可以在回調函數中通過return對遍歷的數組進行處理,生成一個新的數組返回
註釋部分爲特別關注!!!
<script src="../jquery.js"></script>
<script>
var arr = [1,2,3,4,5];
var obj = {0:7, 1:8, 2:9,length:3};
//map和each的區別2
//each靜態方法不支持在回調函數中對遍歷的數組進行處理
//map靜態方法可以在回調函數中通過return對遍歷的數組進行處理,生成一個新的數組返回
var a = $.map(obj,function(value,index){
console.log(index,value);
return value + index;
})
var b = $.each(obj,function(index,value){
console.log(index,value);
return value + index;
})
console.log(a);
console.log(b);
</script>
jQuery的trim()方法
(1)trim()方法用於去掉字符串兩邊的空格
(2)參數: 需要去除兩邊空格的字符串
(3)返回值: 返回一個去除兩邊空格的新的字符串
註釋部分爲特別關注!!!
<script src="../jquery.js"></script>
<script>
var str = " a "
console.log("我是原來的str:"+str);
//trim()去掉字符串兩邊的空格
//參數:需要去除兩邊空格的字符串
//返回值:返回一個去除兩邊空格的新的字符串
var newstr = $.trim(str);
console.log("我是去掉兩邊空格的str:"+newstr);
</script>
jQuery中的isWindow()、isArray()和isFunction()方法
jQuery中的isWindow()、isArray()和isFunction()方法使用方法相類似
(1)isWindow():判斷傳入的對象是否是window對象
(2)isArray():判斷傳入的對象是否是真數組,僞數組返回false
(3)isFunction():判斷傳入的對象是否是一個函數
(4)返回值都是true/false
(5)特別注意: jQuery本質是一個函數
註釋部分爲特別關注!!!
<script src="../jquery.js"></script>
<script>
var str = " a ";
//判斷傳入的對象是否是window對象
//返回值:true/false
var w = window; //windom對象
console.log($.isWindow(str));
console.log($.isWindow(w));
//判斷傳入的對象是否是真數組
//返回值:true/false
var arr = [1,2,3];
var obj = {0:1, 1:2, length:2};
console.log($.isArray(arr));
console.log($.isArray(obj));
//判斷傳入的對象是否是一個函數
//返回值:true/false
//注意:jQuery本質是一個函數
var f = function(){};
console.log($.isFunction(f));
console.log($.isFunction(jQuery));
</script>
jQuery中的holdReady()方法
holdReady()中爲 true 時,暫停 ready 入口函數執行。holdReady()中爲 false 時,恢復ready入口函數執行。
以下代碼顯示結果: 點擊按鈕後一次出現"btn"警告框和"ready"警告框
<body>
<button>點我</button>
</body>
<script src="../jquery.js"></script>
<script>
var btn = document.getElementsByTagName("button")[0];
btn.onclick = function() {
alert("btn");
//恢復ready執行
$.holdReady(false);
}
//暫停ready執行
$.holdReady(true);
$(document).ready(function(){
alert("jquery");
})
</script>