1.attr()
兩個(gè)參數(shù),可用作讀取某個(gè)屬性的值,或者修改某個(gè)屬性的值,操作屬性值為非布爾值的屬性
//讀取第一個(gè)div的title屬性
console.log($('div:first').attr('title'))
給所有的div設(shè)置name屬性(value為asdf)
$('div').attr('name', 'asdf')
2.removeAttr()
移除某個(gè)屬性
移除所有div的title屬性
$('div').removeAttr('title')
3.addClass()
給元素添加類名
給所有的div添加class='abc'
$('div').addClass('abc')
4.removeClass()
移除類名
5.html()
得到某個(gè)元素的文本內(nèi)容
得到最后一個(gè)li的標(biāo)簽體文本
console.log($('li:last').html())
設(shè)置第一個(gè)li的標(biāo)簽體為"<h1>mmmmmmmmm</h1>"
$('li:first').html('<h1>mmmmmmmmm</h1>')
6.val()
獲取元素的val值
得到輸入框中的value值
console.log($(':text').val())
將輸入框的值設(shè)置為asdasd
$(':text').val('asdasd')
7.prop()
也是用作修改元素的某些屬性的值,專門操作屬性值為布爾值的屬性
8.offset()
相對頁面左上角的坐標(biāo),并且可以設(shè)置坐標(biāo)
//獲取坐標(biāo)
var offset = $('.div1').offset() //此方法返回的是一個(gè)對象,里面有l(wèi)eft和top兩個(gè)屬性
console.log(offset.left, offset.top)
//設(shè)置坐標(biāo)
$('.div2').offset({
left: 50,
top: 100
})
- position()
相對于父元素左上角的坐標(biāo)
var position = $('.div1').position()
console.log(position.left, position.top)
10.first()
第一個(gè)元素
$lis.first().css('background', 'red')
11.last()
最后一個(gè)元素
$lis.last().css('background', 'red')
12.eq(index|-index)
根據(jù)索引來找到元素
$lis.eq(1).css('background', 'red')
13.filter(selector)
根據(jù)條件來過濾得到元素
ul下li標(biāo)簽中title屬性為hello的
$lis.filter('[title=hello]').css('background', 'red')
14.not(selector)
根據(jù)條件來過濾元素
ul下li標(biāo)簽中title屬性不為hello的
$lis.not('[title=hello]').css('background', 'red')
15.has(selector)
保留包含特定后代的元素
ul下li標(biāo)簽中有span子標(biāo)簽的
$lis.has('span').css('background', 'red')
查找孩子,父親,兄弟,后代元素
16.children()
查找孩子元素
ul標(biāo)簽的第2個(gè)span子標(biāo)簽
$ul.children('span:eq(1)').css('background', 'red')
17.find()
查找后代元素
ul標(biāo)簽的第2個(gè)span后代標(biāo)簽
$ul.find('span:eq(1)').css('background', 'red')
18.parent()
查找父元素
ul標(biāo)簽的父標(biāo)簽
$ul.parent().css('background', 'red')
19.prevAll()
查找前面的所有兄弟元素
id為cc的li標(biāo)簽的前面的所有l(wèi)i標(biāo)簽
var $li = $('#cc')
$li.prevAll('li').css('background', 'red')
20.nextAll()
查找后面的所有兄弟元素
id為cc的li標(biāo)簽的前面的所有l(wèi)i標(biāo)簽
var $li = $('#cc')
$li.nextAll('li').css('background', 'red')
21.siblings()
查找前后所有的兄弟元素
id為cc的li標(biāo)簽的所有兄弟li標(biāo)簽
$li.siblings('li').css('background', 'red')