1渺氧、選擇器
基本
:first :last :even :odd :not :eq()
內(nèi)容
contains
內(nèi)容包含某某某的節(jié)點(diǎn)
$('li:contains("曉")').css('backgroundColor', 'cyan')
has
寫一個(gè)選擇器嫂沉,
// 找li標(biāo)簽,li標(biāo)簽里面有a的節(jié)點(diǎn)
$('li:has(a)').css('backgroundColor', 'cyan')
// 找ul標(biāo)簽缚态,里面有a的節(jié)點(diǎn)磁椒,不論層級(jí)
$('ul:has(a)').css('backgroundColor', 'cyan')
$('li:has(a)')
li里面有a的li屬性
input[name]
有屬性name的input
input[name=user]
name屬性等于user的input
input[name!=user]
name屬性不等于user的input
// 有屬性name的input
$('input[name]').css('backgroundColor', 'cyan')
// 屬性name值為user的input
$('input[name=user]').css('backgroundColor', 'cyan')
// 屬性name值不為user的input
$('input[name!=user]').css('backgroundColor', 'cyan')
// 屬性name值以u(píng)ser開頭的input
$('input[name^=user]').css('backgroundColor', 'cyan')
// 屬性name值以u(píng)ser結(jié)束的input
$('input[name$=user]').css('backgroundColor', 'cyan')
子元素
:first-child
頭子節(jié)點(diǎn)
:last-child
尾子節(jié)點(diǎn)
:nth-child
指定子節(jié)點(diǎn)
// 找到li標(biāo)簽,li是第一個(gè)兒子節(jié)點(diǎn)的li標(biāo)簽
$('li:first-child').css('backgroundColor', 'cyan')
// 找到li標(biāo)簽玫芦,li是最后一個(gè)兒子節(jié)點(diǎn)的li標(biāo)簽
$('li:last-child').css('backgroundColor', 'cyan')
// 找li標(biāo)簽浆熔,找指定下標(biāo)的li標(biāo)簽,這個(gè)下標(biāo)是兒子節(jié)點(diǎn)里面的第幾個(gè)
$('li:nth-child(1)').css('backgroundColor', 'cyan')
2桥帆、樣式添加医增、屬性獲取
css({})
// 可以連寫-鏈?zhǔn)讲僮? $('#lala').css('backgroundColor', 'red').css('fontSize', '30px')
// 可以傳遞一個(gè)js對(duì)象,直接全部修改
$('#lala').css({backgroundColor: 'blue', fontSize: '40px'})
attr()
// 獲取指定節(jié)點(diǎn)的class屬性
console.log($('#lala').attr('class'))
// 只能獲取第一個(gè)符合要求的id屬性
console.log($('.libai').attr('id'))
// 通過eq選擇第二個(gè)符合要求的id屬性
console.log($('.libai:eq(1)').attr('id'))
// 給指定節(jié)點(diǎn)添加屬性
$('#lala').attr('class', 'bai').attr('name', '狗蛋')
removeAttr()
// 將指定節(jié)點(diǎn)的屬性刪除
// $('#lala').removeAttr('class')
prop()
經(jīng)常用來設(shè)置checked环葵、selected等屬性调窍,設(shè)置的值就是true宝冕、false
// 所有下標(biāo)大于1的多選框選中
// $('input:gt(1)').prop('checked', true)
// 將第二個(gè)下拉框設(shè)置為默認(rèn)選中
// $('#se > option:eq(2)').prop('selected', true)
addClass
// 給指定的節(jié)點(diǎn)添加類名
$('#lala').addClass('hei')
removeClass
// 給指定的節(jié)點(diǎn)移除指定的節(jié)點(diǎn)class名 bai
$('#lala').removeClass('bai')
toggleClass
// 有bai這個(gè)class张遭,那就是刪除這個(gè)class,沒有bai這個(gè)class地梨,那就是添加class
$('#lala').toggleClass('bai')
html()
// 讀取或者設(shè)置節(jié)點(diǎn)內(nèi)容菊卷,和innerHTML功能相同
console.log($('#lala').html('醉臥沙場(chǎng)君莫笑,古來征戰(zhàn)幾人回'))
text()
讀取或者設(shè)置節(jié)點(diǎn)內(nèi)容,和innerText功能相同
val()
// 讀取input框里面的內(nèi)容
console.log($('#ip').val())
// 設(shè)置input框里面的內(nèi)容
console.log($('#ip').val('今天中午吃什么呢宝剖?'))
width()
height()
// 讀取指定對(duì)象寬度 不帶px
console.log($('#dudu').width())
// 設(shè)置寬度 不帶px
console.log($('#dudu').width(300))
// 讀取高度
console.log($('#dudu').height())
// 設(shè)置高度
console.log($('#dudu').height(400))
offset()
// 獲取div的top值和left值
console.log($('#dudu').offset().top, $('#dudu').offset().left)
3洁闰、js對(duì)象和jquery對(duì)象轉(zhuǎn)化
js對(duì)象和jquery對(duì)象的函數(shù)不能通用
js對(duì)象和jquery對(duì)象相互轉(zhuǎn)化
var odiv = document.getElementById('dudu')
// js對(duì)象轉(zhuǎn)化jquery對(duì)象
console.log($(odiv).width())
// jquery對(duì)象轉(zhuǎn)化為js對(duì)象
console.log($('#dudu')[0].style.width)
console.log($('.lala')[1].style.width)
4、文檔處理
append
appendTo
// 向父節(jié)點(diǎn)添加子節(jié)點(diǎn)
$('#car').append('<li>本田飛度</li>')
// 通過子節(jié)點(diǎn)調(diào)用万细,添加到父節(jié)點(diǎn)
$('<li>本田飛度</li>').appendTo($('#car'))
prepend
prependTo
// 通過父節(jié)點(diǎn)調(diào)用扑眉,添加到父節(jié)點(diǎn)的最前面
$('#car').prepend('<li>本田飛度</li>')
// 通過子節(jié)點(diǎn)調(diào)用,添加到父節(jié)點(diǎn)的最前面
$('<li>通用別克</li>').prependTo($('#car'))
after
before
// 是兄弟節(jié)點(diǎn)關(guān)系赖钞,在mao節(jié)點(diǎn)后面添加一個(gè)指定節(jié)點(diǎn)
$('#mao').after('<li>鈴木雨燕</li>')
// 是兄弟節(jié)點(diǎn)關(guān)系腰素,在mao節(jié)點(diǎn)前面添加一個(gè)指定節(jié)點(diǎn)
$('#mao').before('<li>鈴木雨燕</li>')
empty
remove
// 清空指定節(jié)點(diǎn)里面的內(nèi)容,節(jié)點(diǎn)還在
$('#mao').empty()
// 原生js中:父節(jié)點(diǎn).removeChild(子節(jié)點(diǎn))
// 通過子節(jié)點(diǎn)直接調(diào)用雪营,刪除當(dāng)前節(jié)點(diǎn)
$('#mao').remove()
5弓千、篩選和查找
eq
$('li').eq(n).css('backgroundColor', 'red')
$('li:eq(0)').css('backgroundColor', 'red')
$('li:eq(' + 0 + ')').css('backgroundColor', 'red')
first
last
// 第一個(gè)li 和:first 一模一樣
$('li').first().css('backgroundColor', 'red')
// 最后一個(gè)li 和:last 一模一樣
$('li').last().css('backgroundColor', 'red')
hasClass
// 判斷有沒有這個(gè)class,有就返回true,沒有返回false
console.log($('li').eq(0).hasClass('wang'))
filter
// 找到所有l(wèi)i献起,過濾出來 .jing 的這些li
$('li').filter('.jing').css('backgroundColor', 'cyan')
slice
// 取出符合要求li里面的第0個(gè)和第1個(gè) [start, end)
$('li').slice(0, 2).css('backgroundColor', 'cyan')
children
// 所有的兒子節(jié)點(diǎn)
$('#nan').children().css('backgroundColor', 'red')
// 兒子節(jié)點(diǎn)中 有 .feng 的節(jié)點(diǎn)
$('#nan').children('.feng').css('backgroundColor', 'red')
find
// 去子孫節(jié)點(diǎn)中查找所有的 .feng 的節(jié)點(diǎn)
$('#nan').find('.feng').css('backgroundColor', 'cyan')
next
nextAll
// 指定對(duì)象的下一個(gè)兄弟節(jié)點(diǎn)
$('#wu').next().css('backgroundColor', 'cyan')
// 指定對(duì)象的后面所有的節(jié)點(diǎn)
$('#wu').nextAll().css('backgroundColor', 'cyan')
prev
指定對(duì)象的上一個(gè)兄弟節(jié)點(diǎn)
prevAll
指定對(duì)象的上面所有的兄弟節(jié)點(diǎn)
parent
parents
// 找到當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)
$('.lin').parent().css('backgroundColor', 'cyan')
// 查找得到所有的上層節(jié)點(diǎn)
$('#qing').parents().css('backgroundColor', 'cyan')
// 查找得到指定的上層節(jié)點(diǎn)
$('#qing').parents('div').css('backgroundColor', 'cyan')
siblings
// 查找qing節(jié)點(diǎn)所有的兄弟節(jié)點(diǎn)
$('#qing').siblings().css('backgroundColor', 'orange')
// 查找qing節(jié)點(diǎn)所有的兄弟節(jié)點(diǎn)洋访,而且是.lin的兄弟節(jié)點(diǎn)
$('#qing').siblings('.lin').css('backgroundColor', 'orange')
6镣陕、事件
添加事件
$('div).click(function () {})
事件綁定
on off one
綁定事件
$('div').on('click', test1)
$('div').on('click', test2)
取消事件綁定
$('div').off('click', test2)
// 事件只能觸發(fā)一次
$('div').one('click', test2)
取消冒泡
ev.stopPropagation()
阻止默認(rèn)行為
ev.preventDefault()
獲取鼠標(biāo)坐標(biāo)
ev.pageX, ev.pageY
下標(biāo)
// 得到指定jquery對(duì)象在前面數(shù)組中的下標(biāo)
// console.log($('div').index($('#heng')))
7、動(dòng)畫
show()
// 參數(shù)1:動(dòng)畫的事件
// 參數(shù)2:動(dòng)畫完畢之后執(zhí)行的函數(shù)
$('#dudu').show(5000, function () {
alert('菊花殘,滿地傷')
})
和show一樣hide()
和show一樣
$('#dudu').hide(5000, fn)
slidedown()
原來不顯示姻政,動(dòng)畫下拉顯示
$('#dudu').slideDown(5000)
slideup()
原來顯示呆抑,動(dòng)畫的上拉消失
slideToggler()
如果隱藏就下拉顯示,如果顯示就上拉消失
fadeIn()
// 慢慢的顯示出來
$('#dudu').fadeIn(5000)
fadeOut()
// 慢慢的消失
$('#dudu').fadeOut(5000)
fadeTo()
// 5秒之內(nèi)扶歪,透明度變?yōu)?.01
// $('#dudu').fadeTo(5000, 0.01)
fadeToggle()
如果隱藏就淡入顯示理肺,如果顯示就淡出消失
自定義的動(dòng)畫效果
animate()
// 自定義動(dòng)畫
$('#dudu').animate({width: 1000, height: 500, opacity: 0.1}, 5000)
stop()
//停止動(dòng)畫
$('#dudu').stop()