入口函數(shù)
$(document).ready(function(){});
$(function(){});
js入口函數(shù)跟jQuery入口函數(shù)的區(qū)別:
1.Js的window.onload事件是等到所有內(nèi)容,以及我們的外部圖片之類(lèi)的文件加載完了之后沧踏,才回去執(zhí)行
2.jQuery的入口函數(shù) 是在 html所有標(biāo)簽都加載之后肮韧,就回去執(zhí)行。
選擇器
4.2 選擇器匯總
* $("*") 所有元素
#id $("#lastname") id="lastname" 的元素
.class $(".intro") 所有 class="intro" 的元素
element $("p") 所有 <p> 元素
.class.class $(".intro.demo") 所有 class="intro" 且 class="demo" 的元素
:first $("p:first") 第一個(gè) <p> 元素
:last $("p:last") 最后一個(gè) <p> 元素
:even $("tr:even") 所有偶數(shù) <tr> 元素
:odd $("tr:odd") 所有奇數(shù) <tr> 元素
:eq(index) $("ul li:eq(3)") 列表中的第四個(gè)元素(index 從 0 開(kāi)始)
:gt(no) $("ul li:gt(3)") 列出 index 大于 3 的元素 greater than
:lt(no) $("ul li:lt(3)") 列出 index 小于 3 的元素 less than
:not(selector) $("input:not(:empty)") 所有不為空的 input 元素
:header $(":header") 所有標(biāo)題元素 <h1> - <h6>
:animated 所有動(dòng)畫(huà)元素
:contains(text) $(":contains('W3School')") 包含指定字符串的所有元素
:empty $(":empty") 無(wú)子(元素)節(jié)點(diǎn)的所有元素
:hidden $("p:hidden") 所有隱藏的 <p> 元素
:visible $("table:visible") 所有可見(jiàn)的表格
s1,s2,s3 $("th,td,.intro") 所有帶有匹配選擇的元素
[attribute] $("[href]") 所有帶有 href 屬性的元素
[attribute=value] $("[href='#']") 所有 href 屬性的值等于 "#" 的元素
[attribute!=value] $("[href!='#']") 所有 href 屬性的值不等于 "#" 的元素
[attribute$=value] $("[href$='.jpg']") 所有 href 屬性的值包含以 ".jpg" 結(jié)尾的元素
:input $(":input") 所有 <input> 元素
:text $(":text") 所有 type="text" 的 <input> 元素
:password $(":password") 所有 type="password" 的 <input> 元素
:radio $(":radio") 所有 type="radio" 的 <input> 元素
:checkbox $(":checkbox") 所有 type="checkbox" 的 <input> 元素
:submit $(":submit") 所有 type="submit" 的 <input> 元素
:reset $(":reset") 所有 type="reset" 的 <input> 元素
:button $(":button") 所有 type="button" 的 <input> 元素
:image $(":image") 所有 type="image" 的 <input> 元素
:file $(":file") 所有 type="file" 的 <input> 元素
:enabled $(":enabled") 所有激活的 input 元素
:disabled $(":disabled") 所有禁用的 input 元素
:selected $(":selected") 所有被選取的 input 元素
:checked $(":checked") 所有被選中的 input 元素
DOM對(duì)象跟jQuery對(duì)象相互轉(zhuǎn)換
- jQuery對(duì)象轉(zhuǎn)換成DOM對(duì)象:
方式一:$(“#btn”)[0]
方式二:$(“#btn”).get(0) - DOM對(duì)象轉(zhuǎn)換成jQuery對(duì)象:
$(document) -> 把DOM對(duì)象轉(zhuǎn)成了jQuery對(duì)象
var btn = document.getElementById(“bt n”);
btn -> $(btn);
4.3 jQuery選擇方法
- 4.3.1 獲取父級(jí)元素
* $(selector).parent(); //獲取直接父級(jí)
* $(selector).parents('p'); //獲取所有父級(jí)元素直到html
- 4.3.2 獲取子代和后代的元素
* $(selector).children(); //獲取直接子元素
* $(selector).find("span"); //獲取所有的后代元素
* find方法 可能用的多。
- 4.3.3 獲取同級(jí)的元素
* $(selector).siblings() //所有的兄弟節(jié)點(diǎn)
* $(selector).next() //下一個(gè)節(jié)點(diǎn)
* $(selector).nextAll() //后面的所有節(jié)點(diǎn)
* $(selector).prev() //前面一個(gè)的兄弟節(jié)點(diǎn)
* $(selector).prevAll() //前面的所有的兄弟節(jié)點(diǎn)
- 4.3.4 過(guò)濾方法
* $("div p").last(); //取最后一個(gè)元素
* $("div p").first(); //取第一個(gè)元素
* $("p").eq(1); //去第n個(gè)元素
* $("p").filter(".intro"); //過(guò)濾竟秫,選擇所有p標(biāo)簽帶有 .intro類(lèi)
$('p.intro')
* $("p").not(".intro"); //去除陪竿,跟上面的filetr正好相反
5.jQuery的Dom操作
5.1 獲取html的內(nèi)容
$(selector).text() - 設(shè)置或返回所選元素的文本內(nèi)容
$(selector).html() - 設(shè)置或返回所選元素的內(nèi)容(包括 HTML 標(biāo)記)
$(selector).val() - 設(shè)置或返回表單字段的值
- 獲取和設(shè)置相同方法名,通過(guò)不同參數(shù)來(lái)確定是獲取還是設(shè)置值
$("#blin").text("傳智播客");
var txt = $("#blin").text();
使用html來(lái)創(chuàng)建dom的方式效率比較高。 遠(yuǎn)大于: document.createElement();
案例:02動(dòng)態(tài)創(chuàng)建表格.html
5.2 樣式操作
- 5.2.1 基本樣式操作
$(selector).css("color","red") |css({}) 設(shè)置或返回匹配元素的樣式屬性简卧。
$(selector).height() 設(shè)置或返回匹配元素的高度兔魂。
$(selector).offset().left => { left:99, top: 22} 返回第一個(gè)匹配元素相對(duì)于文檔的位置。left,top
$(selector).offsetParent() 返回最近的定位祖先元素举娩。
$(selector).position() 返回第一個(gè)匹配元素相對(duì)于父元素的位置析校。
$(window).scrollLeft() 設(shè)置或返回匹配元素相對(duì)滾動(dòng)條左側(cè)的偏移构罗。
$(window).scrollTop(0) 設(shè)置或返回匹配元素相對(duì)滾動(dòng)條頂部的偏移。
<!-- onscroll -->
$(selector).on("scroll",function(){});
$(selector).width() 設(shè)置或返回匹配元素的寬度智玻。
- 5.2.2 樣式類(lèi)操作盡量操作樣式類(lèi)遂唧,少直接操作css屬性
$(selector).addClass('class'); 向匹配的元素添加指定的類(lèi)名。
$(selector).removeClass('class'); 從所有匹配的元素中刪除全部或者指定的類(lèi)吊奢。
$(selector).toggleClass('class') 從匹配的元素中添加或刪除一個(gè)類(lèi)蠢箩。
$(selector).hasClass('class') 檢查匹配的元素是否擁有指定的類(lèi)。
5.3 屬性操作
$(selector).attr("id") 設(shè)置或返回匹配元素的屬性和值
$(selector).removeAttr()從所有匹配的元素中移除指定的屬性事甜。
5.4 動(dòng)態(tài)創(chuàng)建
$(selector).append() - 在被選元素的結(jié)尾插入內(nèi)容
$(selector).append(node)
$(selector).append('<div></div>')
$(selector).appendTo(); - 追加到..
$(selector).prepend() - 在被選元素的開(kāi)頭插入內(nèi)容
$(selector).after() - 在被選元素之后插入內(nèi)容
$(selector).before() - 在被選元素之前插入內(nèi)容
案例04城市選擇案例.html
6. 事件處理
6.1 簡(jiǎn)單事件綁定方法
.click(hander) .click() //綁定事件 或者觸發(fā) click事件
.blur() //失去焦點(diǎn)事件谬泌,同上
.hover(mousein, mouseleave) //鼠標(biāo)移入,移出
mouseout: 當(dāng)鼠標(biāo)離開(kāi)元素及它的子元素的時(shí)都會(huì)觸發(fā)逻谦。
mouseleave: 當(dāng)鼠標(biāo)離開(kāi)自己時(shí)才會(huì)觸發(fā)掌实,子元素不觸發(fā)。
.dbclick() 雙擊
change 改變,比如:文本框發(fā)送改變邦马,下來(lái)列表發(fā)生改變等...
focus 獲得焦點(diǎn)
keyup, keydown, keypress : 鍵盤(pán) 鍵被按下贱鼻。
mousedown, mouseover
6.2 綁定事件的方式 bind方式(不推薦,1.7以后的jQuery版本被on取代)
- 語(yǔ)法格式:.bind( eventType [, eventData ], handler )
- 參數(shù)說(shuō)明
- 第一個(gè)參數(shù):事件類(lèi)型
- 第二個(gè)參數(shù):傳遞給事件響應(yīng)方法的數(shù)據(jù)對(duì)象滋将,可以省略邻悬。
- 事件響應(yīng)方法中獲取數(shù)據(jù)方式: e.data
- 第三個(gè)參數(shù):事件響應(yīng)方法
第二個(gè)參數(shù)可以省略。
例如:
$("p").bind("click", function(e){
//事件響應(yīng)方法
});
$("p").on('click',function(e){
//事件響應(yīng)方法
})
6.3 delegate方式(推薦随闽,性能高父丰,支持動(dòng)態(tài)創(chuàng)建的元素)
* 語(yǔ)法格式:$(selector).delegate( selector, eventType, handler )
* 語(yǔ)法說(shuō)明:
- 第一個(gè)參數(shù):selector, 子選擇器
- 第二個(gè)參數(shù):事件類(lèi)型
- 第三個(gè)參數(shù):事件響應(yīng)方法
```
例如:
$(".parentBox").delegate("p", "click", function(){
//為 .parentBox下面的所有的p標(biāo)簽綁定事件
});
$(".parentBox").on("click","p", function(){
//為 .parentBox下面的所有的p標(biāo)簽綁定事件
});
```
*優(yōu)勢(shì):效率較高*
6.4 one綁定一次事件的方式
* .one( events [, data ], handler )
例如:
$( "p" ).one( "click", function() {
alert( $( this ).text() );
});
$("p").on("click",function(){
$(this).off('click');//事件方法執(zhí)行了一次后掘宪,就立即解綁事件
})
6.5 on綁定的方式(整合了bind蛾扇、delegate 烈建議使用的方式))
- jQuery1.7版本后,jQuery用on統(tǒng)一了所有的事件處理的方法
- 語(yǔ)法格式:$(selector).on( events [, selector ] [, data ], handler )
- 參數(shù)介紹:
- 第一個(gè)參數(shù):events魏滚,事件名
- 第二個(gè)參數(shù):selector,類(lèi)似delegate
- 第三個(gè)參數(shù): 傳遞給事件響應(yīng)方法的參數(shù)
- 第四個(gè)參數(shù):handler镀首,事件處理方法
例如:
//綁定一個(gè)方法
$( "#dataTable tbody tr" ).on( "click", function() {
console.log( $( this ).text() );
});
//給子元素綁定事件
$( "#dataTable tbody" ).on( "click", "tr", function() {
console.log( $( this ).text() );
});
//綁定多個(gè)事件的方式
$( "div.test" ).on({
click: function() {
$( this ).toggleClass( "active" );
}, mouseenter: function() {
$( this ).addClass( "inside" );
}, mouseleave: function() {
$( this ).removeClass( "inside" );
}
});
6.6 解綁
-
unbind解綁 bind方式綁定的事件( 在jQuery1.7以上被 on和off代替)
- $(selector).unbind(); //解綁所有的事件
- $(selector).unbind("click"); //解綁指定的事件
-
undelegate解綁delegate事件
- $( "p" ).undelegate(); //解綁所有的delegate事件
- $( "p" ).undelegate( "click" ); //解綁所有的click事件
-
off解綁on方式綁定的事件
- $( "p" ).off();
- $("P").off('click');
- $( "p" ).off( "click", "**" ); // 解綁所有的click事件,兩個(gè)*表示所有
- $( "body" ).off( "click", "p", foo );
6.7 觸發(fā)事件
- 6.7.1 簡(jiǎn)單事件觸發(fā)
- $(selector).click(); //觸發(fā) click事件
- 6.7.2 trigger方法觸發(fā)事件
- $( "#foo" ).trigger( "click" );
- 6.7.3 triggerHandler觸發(fā) 事件響應(yīng)方法鼠次,不觸發(fā)瀏覽器行為
- $( "input" ).triggerHandler( "focus" );
6.8 event對(duì)象的簡(jiǎn)介
event.data //傳遞的額外事件響應(yīng)方法的額外參數(shù)
event.currentTarget === this //在事件響應(yīng)方法中等同于this更哄,當(dāng)前Dom對(duì)象
**event.target ** //事件觸發(fā)源,不一定===this
event.pageX //The mouse position relative to the left edge of the document
event.pageY
event.stopPropagation()//阻止事件冒泡
e.preventDefault(); //阻止默認(rèn)行為
event.type //事件類(lèi)型:click腥寇,dbclick...
event.which //鼠標(biāo)的按鍵類(lèi)型:左1 中2 右3
keydown : a,b,c
event.keyCode// code的c是大寫(xiě)
7. jQuery動(dòng)畫(huà)系統(tǒng)
7.1隱藏顯示
* $(selector).show(speed,callback);
* $(selector).hide(1000);
* $(selector).toggle("slow");
* 三個(gè)方法的語(yǔ)法都一致成翩,參數(shù)可以有兩個(gè),第一個(gè)是動(dòng)畫(huà)的速度花颗,第二個(gè)是動(dòng)畫(huà)執(zhí)行完成后的回調(diào)函數(shù)捕传。
* 第一個(gè)參數(shù)是:可以是單詞或者毫秒數(shù)
7.2淡入淡出
$(selector).fadeIn(speed, callback)
$(selector).fadeOut(1000)
$(selector).fadeToggle('fast',function(){})
參數(shù)等同于 7.1
* $(selector).fadeTo(.5); //淡入到 0透明,1不透明
7.3滑動(dòng)
* $(selector).slideDown(speed,callback);
* $(selector).slideUp(speed,callback);
* $(selector).slideToggle(speed,callback);
7.4動(dòng)畫(huà)
* $(selector).animate({params},speed,callback);
$("button").click(function(){
$("div").animate({
left:'250px',
opacity:'0.5',
height:'150px',
width:'150px'
},2000);
}).animate({},1000);
7.5結(jié)束動(dòng)畫(huà)
* $(selector).stop()
* $(selector).stop(stopAll,goToEnd);
- 案例:
8. jQuery補(bǔ)充
- 8.1 each函數(shù)
- 全局的
- $.each(array, function(index, object){})
- 普通jQuery對(duì)象的each方法
- $("li").each(function(index, element){} )
- 參數(shù)的順序是一致的扩劝。
- 全局的