1. jQuery是一個(gè)JavaScript庫(kù)
提供強(qiáng)大的選擇器新翎、簡(jiǎn)潔的API、優(yōu)雅的鏈?zhǔn)骄〕⒈憬莸牟僮鞫詈伲诵睦砟?The Write Less,Do More(寫(xiě)更少瘸恼,做更多)。包括3個(gè)大版本:
- 1字開(kāi)頭的:做了很多低版本IE兼容處理的;
- 2字開(kāi)頭的:基本拋棄了低版本IE(9以下);
- 3字開(kāi)頭的 :2開(kāi)頭的基礎(chǔ)上加了些新的功能
http://jquery.com/ 英文站
http://www.jquery123.com/ 中文站
2. jQuery獲取元素
- $(callBack)
- $(document).ready(callBack)
- $( ).ready(callBack)
這三種方法就相當(dāng)于JS中的window.onload用于獲取加載頁(yè)面中的元素册养,三種方法可以同時(shí)在一個(gè)頁(yè)面中存在东帅。
<pre>$(function(){
console.log($('input')); //length:2
console.log($('#box')); //length:1
console.log($('span','#div1')); //從div1身上去獲取span標(biāo)簽
console.log($('li',$('.list')));
console.log($('.list li'));
console.log($('.list span')); //length:0
//用原生js獲取到的元素不能使用jquery里的方法,如果想用的話(huà)那就把原生js獲取到的元素轉(zhuǎn)成jquery的形式.
var div2=document.getElementById("div2");
$(div2).css('background','red');//把原生js的對(duì)象包裝成jquery的對(duì)象
});
</script>
</head>
<body>
<input type="button" value="按鈕" />
<input type="button" value="按鈕" />
<div id="box">123</div>
<div id="div1">
<span>Wendaoliu</span>
</div>
<ul class="list">
<li>Wendaoliu1</li>
</ul>
<div id="div2">Wendaoliu2</div>
</body> </pre>
3. jQuery中的事件
jquery里的事件(沒(méi)有on)。
語(yǔ)法:$(元素).click(callBack);
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('#btn').click(function(){
console.log(this); //原生的this一樣是指向觸發(fā)事件的對(duì)象
console.log($(this)); //指向jquery的對(duì)象
});
$('#box').mouseover(function(){
$(this).css('background','green');
});
});
</script>
</head>
<body>
<input type="button" id="btn" value="按鈕" />
<div id="box"></div>
</body></pre>
4. jQuery中CSS方法
css( )關(guān)于樣式style的一些操作:
1個(gè)參數(shù):獲取屬性的值
參數(shù)為一個(gè)對(duì)象的話(huà)球拦,表示設(shè)置屬性靠闭,可以同時(shí)設(shè)置多個(gè)屬性的值
2個(gè)參數(shù):設(shè)置屬性的值
第一個(gè)參數(shù)是屬性名;第二個(gè)參數(shù)為屬性名對(duì)應(yīng)的值
表單的value值:
val( ):獲取表單對(duì)應(yīng)的value值
val(values):設(shè)置表單的value值
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
console.log($('#box').css('width')); //100px
$('#box').css('width',200); //設(shè)置box的width為200
$('#btn').click(function(){
$('#box').css({width:200,height:200,background:'green'});
});
console.log($('#btn').val()); //按鈕
$('#btn').val('Wen'); //有參數(shù)的話(huà)就是設(shè)置value值
});
</script>
</head>
<body>
<input type="button" id="btn" value="按鈕" />
<div id="box"></div>
</body></pre>
5. jQuery中的attr( )、data( )坎炼、html( )愧膀、text( )和鏈?zhǔn)讲僮?/h2>
-
attr( ):關(guān)于標(biāo)簽屬性的操作
一個(gè)參數(shù):獲取元素的某個(gè)屬性,參數(shù)的值就是屬性名字;
兩個(gè)參數(shù):設(shè)置元素的某個(gè)屬性谣光,第一個(gè)參數(shù)為屬性名字檩淋,第二個(gè)參數(shù)為屬性要設(shè)置成的值。
-
data( )給標(biāo)簽添加數(shù)據(jù)
有參數(shù)的話(huà)萄金,是添加數(shù)據(jù)(用對(duì)象的形式去表示)蟀悦;沒(méi)參數(shù)的話(huà),是獲取數(shù)據(jù)
-
html( )取元素里的內(nèi)容氧敢,與js中的innerHTML的效果是一樣的
-
text( )取元素里的文字日戈,不取標(biāo)簽
-
鏈?zhǔn)讲僮?/strong>:對(duì)一個(gè)元素進(jìn)行連續(xù)的操作
一個(gè)參數(shù):獲取元素的某個(gè)屬性,參數(shù)的值就是屬性名字;
兩個(gè)參數(shù):設(shè)置元素的某個(gè)屬性谣光,第一個(gè)參數(shù)為屬性名字檩淋,第二個(gè)參數(shù)為屬性要設(shè)置成的值。
有參數(shù)的話(huà)萄金,是添加數(shù)據(jù)(用對(duì)象的形式去表示)蟀悦;沒(méi)參數(shù)的話(huà),是獲取數(shù)據(jù)
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
console.log($('p').attr('class')); //con
$('p').attr('class','text');
$('input').attr('value','按鈕'); //這種方與val()這個(gè)方法的效果是一樣的
$('input').attr('name',123); //可以添加自定義屬性
console.log($('input').attr('name')); //123
$('p').data({'name':'Wendaoliu','age':25});
console.log($('p').data());
});
</script>
</head>
<body>
<p class="con">Wendaoliu</p>
<input type="button" value="按鈕" />
</body></pre>
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
console.log($('div').html( )); //這是<span>一個(gè)</span>無(wú)名<span>之地</span>
console.log($('div').text( )); //這是一個(gè)無(wú)名之地
$('div').html('<p><span>Wendaoliu</span></p>');
$('div').text('Wendaoliu123');
})
</script>
</head>
<body>
<div>這是<span>一個(gè)</span>無(wú)名<span>之地</span></div>
</body></pre>
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('div').css({width:100,height:100,background:'red'}).html('<span>kaivon</span>').click(function(){
$(this).css('color','blue');
});
});
</script>
</head>
<body>
<div><span>這是一個(gè)無(wú)名之地</span></div>
</body></pre>
6. jQuery中節(jié)點(diǎn)的操作
- 子節(jié)點(diǎn)
.first( ):獲取第一個(gè)子節(jié)點(diǎn);
.last( ):獲取最后一個(gè)子節(jié)點(diǎn)孙乖;
.slice(start,end):截取部分子節(jié)點(diǎn)浙炼。start 起始位置;end結(jié)束位置,不包含結(jié)束位置的圆,第二個(gè)參數(shù)不寫(xiě)的話(huà)鼓拧,截到最后一位。
.children( ):獲取到元素里的第一層子節(jié)點(diǎn)越妈。參數(shù)如果沒(méi)有的話(huà)季俩,獲取到父級(jí)下的所有子節(jié)點(diǎn);有參數(shù)的話(huà)梅掠,參數(shù)是一個(gè)選擇器酌住,找到對(duì)應(yīng)選擇器的節(jié)點(diǎn)。
.find( ):獲取元素里的所有節(jié)點(diǎn)阎抒;
.prev( ):上一個(gè)兄弟節(jié)點(diǎn)酪我;
.next( ):上一個(gè)兄弟節(jié)點(diǎn);
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('li').first().css('background','red');
//$('li').last().css('background','green');
$('li').slice(2,4).css('background','yellow');
$('li').slice(4).css('background','blue');
});
</script></pre>
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('ul').children().css('color','red');
$('ul').children('.blue').css('color','blue');
$('ul').find('li').css('background','pink');
$('ul').find('span').css('background','black');
})
</script>
</head>
<body>
<ul> <li>red</li> <li>green</li> <li class="blue">blue</li> <li>yellow</li> <li>pink<span>black</span></li> </ul>
</body></pre>
- 父節(jié)點(diǎn)
.parent( ):元素的第一層父節(jié)點(diǎn)
.parents( ):元素的所有父節(jié)點(diǎn)且叁,它會(huì)一直往上找都哭,直到找到html
.closest():從自身開(kāi)始找,一層一層往外找,找到最近滿(mǎn)足條件的元素欺矫,找到一個(gè)以后就不再找了纱新。 - 節(jié)點(diǎn)操作
父級(jí).append(要添加的元素):把要添加的元素添加到父級(jí)的最后面;
父級(jí).prepend(要添加的元素):把要添加的元素添加到父級(jí)的最前面穆趴;
元素.before(要添加的元素):把要添加的元素添加到一個(gè)指定的元素的前面脸爱;
元素.after(要添加的元素):把要添加的元素添加到一個(gè)指定的元素的后面;
要添加的元素.appendTo(父級(jí)):把要添加的元素添加到父級(jí)的最后面未妹;
要添加的元素.prependTo(父級(jí)):把要添加的元素添加到父級(jí)的最前面簿废;
要添加的元素.insertBefore(元素):把要添加的元素添加到一個(gè)指定的元素的前面;
要添加的元素.insertAfter(元素):把要添加的元素添加到一個(gè)指定的元素的后面络它;
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
var li1=$('<li>red</li>'); var li2=$('<li>green</li>'); var div3=$('<div>blue</div>'); var div4=$('<div>yellow</div>');
$('#list').append(li1);
$('#list').prepend(li2);
$('#list').before(div3);
$('#list').after(div4);
});
</script>
</head>
<body>
<ul id="list"></ul>
</body></pre>
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
var li1=$('<li>red</li>'); var li2=$('<li>green</li>'); var div3=$('<div>blue</div>'); var div4=$('<div>yellow</div>');
li1.appendTo($('#list'));
li2.prependTo($('#list'));
div3.insertBefore($('#list'));
div4.insertAfter($('#list'));
});
</script>
</head>
<body>
<ul id="list"></ul>
</body></pre>
4.remove( ) 刪除元素:元素.remove()族檬;
clone(blooen) 克隆元素:參數(shù)默認(rèn)為空,表示只復(fù)制元素酪耕,不復(fù)制事件导梆。如果參數(shù)為true,表示元素與事件都會(huì)被復(fù)制迂烁。
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('li').first().remove();
$('span').click(function(){
alert(1);
});
var newSpan=$('span').clone(true);
newSpan.appendTo($('div'));
});
</script>
</head>
<body>
`<ul>
<li>red</li>
<li>green</li>
<li>blue</li>
</ul>`
<span>點(diǎn)擊我</span>
<div></div>
</body></pre>
7.索引看尼、循環(huán)、get( )盟步、wrap( )
-
index( )
沒(méi)有參數(shù):第一個(gè)元素(獲取到的這個(gè)元素藏斩,因?yàn)閖query獲取到的是一組數(shù)據(jù))在兄弟元素中的排行;
有參數(shù):代表前面的元素在參數(shù)的標(biāo)簽(所有標(biāo)簽,不分兄弟)里排行第幾却盘。
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
console.log($('#div1').index()); //0
console.log($('div').index()); //1
console.log($('span').index()); //4
console.log($('#s1').index('span')); //2
console.log($('#s2').index('span')); //5
console.log($('.box').index('span')); //-1表示沒(méi)有找到
});
</script>
</head>
<body>
<p>p</p>
<div>div</div>
<div>div</div>
<div>
<div id="div1"></div>
</div>
<span></span>
<span></span>
<div class="box">
<span id="s1">s1</span>
<span></span>
<span></span>
</div>
<div><span id="s2">s2</span></div>
</body></pre> - 循環(huán)each(i,elem)
i:每個(gè)元素對(duì)應(yīng)的下標(biāo)(索引)狰域;
elem:每個(gè)元素,原生的元素黄橘;
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('li').each(function(i,elem){
console.log(i,elem);
console.log(this==elem); //true
if(i==3){
return false; //跳出循環(huán)
}
$(elem).html(i); //elem是一個(gè)原生的對(duì)象兆览,需要要轉(zhuǎn)成jquery的對(duì)象
});
});
</script>
</head>
<body>
<ul> <li></li> <li></li> <li></li> <li></li> <li></li> </ul>
</body></pre> - get(index):把jquery對(duì)象轉(zhuǎn)成原生對(duì)象,index是元素的索引值塞关。原生對(duì)象轉(zhuǎn)jquery對(duì)象用$抬探。
eq(index) :參數(shù)為下標(biāo),通過(guò)索引找到對(duì)應(yīng)的元素帆赢。
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
console.log($('#div1').html()); //Wendaoliu
console.log($('#div1').innerHTML); //undefined
console.log($('div').get(0).innerHTML); //Wendaoliu
console.log($('div').get(1).innerHTML); //Wendaodao
console.log($('div').eq(1).html()); //Wendao
})
</script>
</head>
<body>
<div id="div1">Wendaoliu</div>
<div id="div1">Wendao</div>
<div>Wen</div>
</body></pre> -
wrap( ):在標(biāo)簽外在加一層父級(jí) 小压;
wrapAll( ):把所有的標(biāo)簽都拿過(guò)來(lái)放在一起,然后在他們的外面加一個(gè)父級(jí)椰于;
wrapInner( ):在父級(jí)的里面添加一個(gè)標(biāo)簽怠益,并把內(nèi)容放到這個(gè)標(biāo)簽里面;
unwrap( ):刪除父級(jí)(不包含body標(biāo)簽)瘾婿;
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('input').click(function(){
if($(this).val()=='管理員'){
$('span').wrap('<a href="#"></a>');
}else{
$('span').unwrap();
}
});
})
</script>
</head>
<body>
<input type="radio" value="管理員" name="user" />管理員
<input type="radio" value="普通用戶(hù)" name="user" />普通用戶(hù)
<span>登錄</span>
</body></pre>
8. jQuery中的尺寸
元素的尺寸
width( ):獲取元素的寬度(值不帶單位)蜻牢,有參數(shù)的話(huà)烤咧,代表設(shè)置寬度,參數(shù)不用帶單位孩饼;
height( ):獲取元素的高度髓削;
innerWidth( ):width+padding
innerHeight( ):height+padding
outerWidth( ):width+padding+border
outerHeight( ):height+padding+border
<pre><style>
#box{
width: 100px;
height: 150px;
background: red;
padding: 10px;
margin: 10px;
border: 1px solid #000;
}
</style>
<script src="jquery-3.1.0.js"></script>
<script>
$(function(){
console.log($('#box').width()); //100
console.log($('#box').height()); //150
console.log($('#box').innerWidth()); //120
console.log($('#box').innerHeight()); //170
console.log($('#box').outerWidth()); //122
console.log($('#box').outerHeight()); //172
$('#box').width(200);
$('#box').height(300);
console.log($('#box').get(0).offsetWidth); //222
});
</script>
</head>
<body>
<div id="box"></div>
</body></pre>-
可視區(qū)的尺寸
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
//頁(yè)面的寬高
console.log($(document).width());
console.log($(document).height());//可視區(qū)的寬高 console.log($(window).width()); console.log($(window).height()); }); </script>
</head>
<body style="height: 2000px;">
</body></pre> 元素相對(duì)于文檔的距離:
offset( ).left
offset( ).top
元素相對(duì)于父級(jí)的距離:
position( ).left
position( ).top
position的值是不會(huì)受margin影響的,或者說(shuō)它的值不加margin镀娶。
offsetParent( ):找到最近的有定位的父級(jí)
<pre><style>
*{
margin: 0;
padding: 0;
}
#div1{
width: 200px;
height: 200px;
background: red;
position: absolute;
left: 50px;
top: 50px;
}
#div2{
width: 100px;
height: 100px;
background: green;
margin: 50px;
position: absolute;
left: 20px;
top: 20px;
}
</style>
<script src="jquery-3.1.0.js"></script>
<script>
$(function(){
console.log($('#div1').offset().top); //50
console.log($('#div1').offset().left); //50
console.log($('#div2').offset().top); //120
console.log($('#div2').offset().left); //120
console.log($('#div2').position().left); //20
console.log($('#div2').position().top); //20
//綠色塊離紅色塊的真正距離
console.log($('#div2').offset().left-$('#div1').offset().left);//70
console.log($('#div2').offsetParent()); //div1
})
</script>
</head>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body></pre>
滾動(dòng)條的距離:scrollTop()、scrollLeft()如果有參數(shù)揪罕,就代表設(shè)置滾動(dòng)條的距離梯码。
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
console.log($(document).scrollTop());
$(document).scrollTop(500);
})
</script>
</head>
<body style="height: 2000px;">
</body></pre>
9. jQuery事件
<pre><style>
#box{
width: 100px;
height: 100px;
background: #f00;
}
</style>
<script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('#box').click(function(){
console.log(1);
});
$('#box').on('click',function(){
console.log(3);
});
$('#box').on('click mouseover',function(){
console.log(4);
});
$('#box').on('click',function(){
console.log(5);
$(this).off(); //移除事件
});
});
</script>
</head>
<body>
<div id="box"></div>
</body></pre>
clientX:不帶滾動(dòng)條的距離;pageX:帶滾動(dòng)條的距離好啰。
<pre><style>
#box{
width: 100px;
height: 100px;
background: #f00;
}
</style>
<script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('#box').click(function(ev){
console.log(ev.clientY);
console.log(ev.pageY);
});
$(document).keydown(function(ev){
console.log(ev.keyCode);
});
});
</script>
</head>
<body style="height: 2000px;">
<div id="box"></div>
</body></pre>
事件委托
<pre><script src="jquery-3.1.0.js"></script>
<script>
//第二個(gè)參數(shù)是把事件添加到誰(shuí)的身上
$(function(){
$('ul').on('click','li',function(){
//這里面的this指向第二個(gè)參數(shù)
console.log($(this));
$(this).css('background','red');
});
$('input').click(function(){
var li=$('<li>2222</li>');
$('ul').append(li); //li.appendTo($('ul'));
});
});
</script>
</head>
<body>
<input type="button" value="添加" />
<ul style="border: 1px solid #f00;"> <li>111</li> <li>111</li> <li>111</li> <li>111</li> <li>111</li> </ul>
</body></pre>
自定義事件
<pre><script src="jquery-3.1.0.js"></script>
<script>
$(function(){
$('span').on('點(diǎn)擊我',function(){
console.log('被你寵幸了');
});
$('div').on('鼠標(biāo)移入',function(){
console.log('你進(jìn)去了');
});
$('span').click(function(){
$('span').trigger('點(diǎn)擊我');
});
$('div').mouseover(function(){
$(this).trigger('鼠標(biāo)移入');
});
});
</script>
</head>
<body>
<div><span>點(diǎn)擊</span></div>
</pre>
10.jQuery工具方法轩娶、運(yùn)動(dòng)
<pre>1.$(選擇器).css() $(選擇器).html() $(選擇器).trigger()
以上這些都是jquery里的方法,原生的不能直接用框往;
2.$.xxx() $.yyy()
這些方法是jquery里的方法鳄抒,原生的也可以直接用。
$.type():檢測(cè)參數(shù)的類(lèi)型
http://www.jquery123.com/jQuery.type/
$.isFunction():檢測(cè)參數(shù)是不是函數(shù)
http://www.jquery123.com/jQuery.isFunction/
$.isNumeric():檢測(cè)參數(shù)是不是個(gè)數(shù)字
http://www.jquery123.com/jQuery.isNumeric/
$.isArray():檢測(cè)參數(shù)是不是數(shù)組
http://www.jquery123.com/jQuery.isArray/
$.isWindow():檢測(cè)參數(shù)是不是window對(duì)象
$.isEmptyObject():檢測(cè)對(duì)象是否為空
$.isPlainObject():檢測(cè)對(duì)象是不是純粹的對(duì)象椰弊,通過(guò){}许溅、new Object()創(chuàng)建的對(duì)象稱(chēng)為純粹的對(duì)象
</pre>
- show( )、hide( )秉版、toggle( )贤重、animate( )方法
- show(duration,easing,complate)顯示
hide(duration,easing,complate)隱藏
toggle(duration,easing,complate) 在兩種效果之間進(jìn)行切換 - 漸隱漸現(xiàn):fadeIn(duration,easing,complate) 顯示
fadeOut(duration,easing,complate)隱藏
fadeToggle(duration,easing,complate) 在兩種效果之間進(jìn)行切換 - slideUp(duration,easing,complate)收縮
slideDown(duration,easing,complate)展開(kāi)
slideToggle(duration,easing,complate)在兩種效果之間進(jìn)行切換 - animate(property,duration,easing,complate)
property 要運(yùn)動(dòng)的屬性,它是一個(gè)對(duì)象清焕。
設(shè)置延遲:delay(time)
停止動(dòng)畫(huà)
stop():只停止當(dāng)前的運(yùn)動(dòng)(屬性)并蝗,后面的運(yùn)動(dòng)還會(huì)接著走;
stop(true):所有的運(yùn)動(dòng)都停了秸妥;
stop(true,true):當(dāng)前的屬性會(huì)馬上到達(dá)目標(biāo)點(diǎn)滚停,后面的運(yùn)動(dòng)不會(huì)走;
finish():所有的屬性都會(huì)馬上到達(dá)目標(biāo)點(diǎn)粥惧,沒(méi)運(yùn)動(dòng)的效果键畴。
參數(shù):
duration:slow-600;normal-400影晓;fast-200
easing:linear,swing(默認(rèn))
complate:運(yùn)動(dòng)完成后執(zhí)行的回調(diào)函數(shù)镰吵。
<pre>$(function(){
var btn=true;
$('input').click(function(){
if(btn){
/$('div').hide(500,'linear',function(){
console.log('隱藏運(yùn)動(dòng)完成了');
});/
$('div').hide(400); //默認(rèn)是沒(méi)有時(shí)間的,需要傳一個(gè)時(shí)間參數(shù)
}else{
$('div').show(400);
}
btn=!btn;
});
})
</script></pre>
<pre>$(function(){
$('input').eq(0).click(function(){
$('#div1').animate({width:400},2000).delay(2000).animate({height:300},2000,function(){
$('#div2').animate({width:400},2000,'swing');
});
});
$('input').eq(1).click(function(){
$('#div3').animate({width:'-=100'},500,'linear');
});
$('input').eq(2).click(function(){
$('#div4').animate({width:'toggle',height:"toggle"},500,'linear');
});
});
</script></pre>
animate需要注意的問(wèn)題:
<pre><script>
$(function(){
$('#div1').mouseover(function(){
$('#div1').stop().animate({width:400,height:400});
}).mouseout(function(){
$('#div1').stop().animate({width:300,height:200});
});
});
</script></pre>