
1、選擇器
基本
:first :last :even :odd :not :eq()
內(nèi)容
contains 內(nèi)容包含某某某的節(jié)點
$('li:contains("曉")').css('backgroundColor', 'cyan')
has 寫一個選擇器,
// 找li標簽,li標簽里面有a的節(jié)點
$('li:has(a)').css('backgroundColor', 'cyan')
// 找ul標簽,里面有a的節(jié)點,不論層級
$('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值以user開頭的input
$('input[name^=user]').css('backgroundColor', 'cyan')
// 屬性name值以user結(jié)束的input
$('input[name$=user]').css('backgroundColor', 'cyan')
子元素
:first-child頭子節(jié)點
:last-child尾子節(jié)點
:nth-child指定子節(jié)點
// 找到li標簽,li是第一個兒子節(jié)點的li標簽
$('li:first-child').css('backgroundColor', 'cyan')
// 找到li標簽,li是最后一個兒子節(jié)點的li標簽
$('li:last-child').css('backgroundColor', 'cyan')
// 找li標簽,找指定下標的li標簽,這個下標是兒子節(jié)點里面的第幾個
$('li:nth-child(1)').css('backgroundColor', 'cyan')
2、樣式添加、屬性獲取
css({})
// 可以連寫-鏈式操作
$('#lala').css('backgroundColor', 'red').css('fontSize', '30px')
// 可以傳遞一個js對象,直接全部修改
$('#lala').css({backgroundColor: 'blue', fontSize: '40px'})
attr()
// 獲取指定節(jié)點的class屬性
console.log($('#lala').attr('class'))
// 只能獲取第一個符合要求的id屬性
console.log($('.libai').attr('id'))
// 通過eq選擇第二個符合要求的id屬性
console.log($('.libai:eq(1)').attr('id'))
// 給指定節(jié)點添加屬性
$('#lala').attr('class', 'bai').attr('name', '狗蛋')
removeAttr()
// 將指定節(jié)點的屬性刪除
// $('#lala').removeAttr('class')
prop()經(jīng)常用來設(shè)置checked、selected等屬性,設(shè)置的值就是true、false
// 所有下標大于1的多選框選中
// $('input:gt(1)').prop('checked', true)
// 將第二個下拉框設(shè)置為默認選中
// $('#se > option:eq(2)').prop('selected', true)
addClass
// 給指定的節(jié)點添加類名
$('#lala').addClass('hei')
removeClass
// 給指定的節(jié)點移除指定的節(jié)點class名 bai
$('#lala').removeClass('bai')
toggleClass
// 有bai這個class,那就是刪除這個class,沒有bai這個class,那就是添加class
$('#lala').toggleClass('bai')
html()
// 讀取或者設(shè)置節(jié)點內(nèi)容,和innerHTML功能相同
console.log($('#lala').html('醉臥沙場君莫笑,古來征戰(zhàn)幾人回'))
text()讀取或者設(shè)置節(jié)點內(nèi)容,和innerText功能相同
val()
// 讀取input框里面的內(nèi)容
console.log($('#ip').val())
// 設(shè)置input框里面的內(nèi)容
console.log($('#ip').val('今天中午吃什么呢?'))
width()
height()
// 讀取指定對象寬度 不帶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對象和jquery對象轉(zhuǎn)化
js對象和jquery對象的函數(shù)不能通用
js對象和jquery對象相互轉(zhuǎn)化
var odiv = document.getElementById('dudu')
// js對象轉(zhuǎn)化jquery對象
console.log($(odiv).width())
// jquery對象轉(zhuǎn)化為js對象
console.log($('#dudu')[0].style.width)
console.log($('.lala')[1].style.width)
4、文檔處理
append
appendTo
// 向父節(jié)點添加子節(jié)點
$('#car').append('<li>本田飛度</li>')
// 通過子節(jié)點調(diào)用,添加到父節(jié)點
$('<li>本田飛度</li>').appendTo($('#car'))
prepend
prependTo
// 通過父節(jié)點調(diào)用,添加到父節(jié)點的最前面
$('#car').prepend('<li>本田飛度</li>')
// 通過子節(jié)點調(diào)用,添加到父節(jié)點的最前面
$('<li>通用別克</li>').prependTo($('#car'))
after
before
// 是兄弟節(jié)點關(guān)系,在mao節(jié)點后面添加一個指定節(jié)點
$('#mao').after('<li>鈴木雨燕</li>')
// 是兄弟節(jié)點關(guān)系,在mao節(jié)點前面添加一個指定節(jié)點
$('#mao').before('<li>鈴木雨燕</li>')
empty
remove
// 清空指定節(jié)點里面的內(nèi)容,節(jié)點還在
$('#mao').empty()
// 原生js中:父節(jié)點.removeChild(子節(jié)點)
// 通過子節(jié)點直接調(diào)用,刪除當前節(jié)點
$('#mao').remove()
5、篩選和查找
eq
$('li').eq(n).css('backgroundColor', 'red')
$('li:eq(0)').css('backgroundColor', 'red')
$('li:eq(' + 0 + ')').css('backgroundColor', 'red')
first
last
// 第一個li 和:first 一模一樣
$('li').first().css('backgroundColor', 'red')
// 最后一個li 和:last 一模一樣
$('li').last().css('backgroundColor', 'red')
hasClass
// 判斷有沒有這個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個和第1個 [start, end)
$('li').slice(0, 2).css('backgroundColor', 'cyan')
children
// 所有的兒子節(jié)點
$('#nan').children().css('backgroundColor', 'red')
// 兒子節(jié)點中 有 .feng 的節(jié)點
$('#nan').children('.feng').css('backgroundColor', 'red')
find
// 去子孫節(jié)點中查找所有的 .feng 的節(jié)點
$('#nan').find('.feng').css('backgroundColor', 'cyan')
next
nextAll
// 指定對象的下一個兄弟節(jié)點
$('#wu').next().css('backgroundColor', 'cyan')
// 指定對象的后面所有的節(jié)點
$('#wu').nextAll().css('backgroundColor', 'cyan')
prev 指定對象的上一個兄弟節(jié)點
prevAll 指定對象的上面所有的兄弟節(jié)點
parent
parents
// 找到當前節(jié)點的父節(jié)點
$('.lin').parent().css('backgroundColor', 'cyan')
// 查找得到所有的上層節(jié)點
$('#qing').parents().css('backgroundColor', 'cyan')
// 查找得到指定的上層節(jié)點
$('#qing').parents('div').css('backgroundColor', 'cyan')
siblings
// 查找qing節(jié)點所有的兄弟節(jié)點
$('#qing').siblings().css('backgroundColor', 'orange')
// 查找qing節(jié)點所有的兄弟節(jié)點,而且是.lin的兄弟節(jié)點
$('#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()
阻止默認行為
ev.preventDefault()
獲取鼠標坐標
ev.pageX, ev.pageY
下標
// 得到指定jquery對象在前面數(shù)組中的下標
// console.log($('div').index($('#heng')))
7、動畫
show()
// 參數(shù)1:動畫的事件
// 參數(shù)2:動畫完畢之后執(zhí)行的函數(shù)
$('#dudu').show(5000, function () {
alert('菊花殘,滿地傷')
})
和show一樣hide() 和show一樣
$('#dudu').hide(5000, fn)
slidedown()原來不顯示,動畫下拉顯示
$('#dudu').slideDown(5000)
slideup()原來顯示,動畫的上拉消失
slideToggler()如果隱藏就下拉顯示,如果顯示就上拉消失
fadeIn()
// 慢慢的顯示出來
$('#dudu').fadeIn(5000)
fadeOut()
// 慢慢的消失
$('#dudu').fadeOut(5000)
fadeTo()
// 5秒之內(nèi),透明度變?yōu)?.01
// $('#dudu').fadeTo(5000, 0.01)
fadeToggle()如果隱藏就淡入顯示,如果顯示就淡出消失
自定義的動畫效果
animate()
// 自定義動畫
$('#dudu').animate({width: 1000, height: 500, opacity: 0.1}, 5000)
stop()
//停止動畫
$('#dudu').stop()