一准验、選擇器
- 基本
:first :last :even :odd :not :eq() - 內(nèi)容
contains 內(nèi)容包含某某某的節(jié)點(diǎn)
has 寫一個選擇器芍躏,
$('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
:last-child
:nth-child
找到li標(biāo)簽茁影,li是第一個兒子節(jié)點(diǎn)的li標(biāo)簽
// $('li:first-child').css('backgroundColor', 'cyan')
找到li標(biāo)簽适刀,li是最后一個兒子節(jié)點(diǎn)的li標(biāo)簽
// $('li:last-child').css('backgroundColor', 'cyan')
找li標(biāo)簽踱葛,找指定下標(biāo)的li標(biāo)簽偶惠,這個下標(biāo)是兒子節(jié)點(diǎn)里面的第幾個
// $('li:nth-child(1)').css('backgroundColor', 'cyan')
二春寿、樣式添加、屬性獲取
- css({})
// 可以連寫-鏈?zhǔn)讲僮?br> // $('#lala').css('backgroundColor', 'red').css('fontSize', '30px')
// 可以傳遞一個js對象忽孽,直接全部修改
//$('#lala').css({backgroundColor: 'blue', fontSize: '40px'}) - attr()
// 獲取指定節(jié)點(diǎn)的class屬性
// console.log($('#lala').attr('class'))
// 只能獲取第一個符合要求的id屬性
// console.log($('.libai').attr('id'))
// 通過eq選擇第二個符合要求的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)
// 將第二個下拉框設(shè)置為默認(rèn)選中
// $('#se > option:eq(2)').prop('selected', true)
addClass
// $('#lala').addClass('hei')
removeClass
// 給指定的節(jié)點(diǎn)添加類名
// $('#lala').addClass('hei')
// 給指定的節(jié)點(diǎn)移除指定的節(jié)點(diǎn)class名 bai
// $('#lala').removeClass('bai')
toggleClass
// 有bai這個class厘线,那就是刪除這個class,沒有bai這個class出革,那就是添加class
// $('#lala').toggleClass('bai')
html()
// 讀取或者設(shè)置節(jié)點(diǎn)內(nèi)容造壮,和innerHTML功能相同
// console.log($('#lala').html('醉臥沙場君莫笑,古來征戰(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()
// 讀取指定對象寬度 不帶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)
三耳璧、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)
四成箫、文檔處理
- append
- appendTo
- prepend
- prependTo
// 向父節(jié)點(diǎn)添加子節(jié)點(diǎn)
// $('#car').append('<li>本田飛度</li>')
// 通過子節(jié)點(diǎn)調(diào)用,添加到父節(jié)點(diǎn)
// $('<li>本田飛度</li>').appendTo($('#car'))
// 通過父節(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)后面添加一個指定節(jié)點(diǎn)
// $('#mao').after('<li>鈴木雨燕</li>')
// 是兄弟節(jié)點(diǎn)關(guān)系召廷,在mao節(jié)點(diǎn)前面添加一個指定節(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()
五欠拾、篩選和查找
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é)點(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
// 指定對象的下一個兄弟節(jié)點(diǎn)
// $('#wu').next().css('backgroundColor', 'cyan')
// 指定對象的后面所有的節(jié)點(diǎn)
// $('#wu').nextAll().css('backgroundColor', 'cyan')prev 指定對象的上一個兄弟節(jié)點(diǎn)
prevAll 指定對象的上面所有的兄弟節(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')
六遮精、事件
- 添加事件
$('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
index
// 得到指定jquery對象在前面數(shù)組中的下標(biāo)
// console.log($('div').index($('#heng')))
七、動畫
- show()
// 參數(shù)1:動畫的事件
// 參數(shù)2:動畫完畢之后執(zhí)行的函數(shù)
$('#dudu').show(5000, function () {
alert('菊花殘,滿地傷')
}) - 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()