Home  >  Article  >  Web Front-end  >  Summary of basic knowledge of jQuery_jquery

Summary of basic knowledge of jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:25:09933browse

1、基础

 jquery对象集:
  $():jquery对象集合

  获取jquery对象集中的元素:

   使用索引获取包装器中的javascript元素:var temp = $('img[alt]')[0]

   使用jquery的get方法获取jquery对象集中的javascript元素:var temp = $('img[alt]').get(0)

   使用jquery的eq方法获取jquery对象集中的jquery对象元素:
    $('img[alt]').eq(0)
    $('img[alt]').first()
    $('img[alt]').last()

  jquery对象集转换成javascript数组:
   var arr = $('label+button').toArray()label后面所有同级button元素,转换成javascript数组

  jquery对象集的索引:
   var n = $('img').index($('img#id')[0])注意:index()参数是javascript元素
   var n = $('img').index('img#id') 等同于上一行 找不到返回-1
   var n = $('img').index()img在同级元素中的索引

  向jquery对象集中添加更多的jquery对象集:   
   使用逗号:$('img[alt],img[title]')
   使用add方法:$('img[alt]').add('img[title]')

   对不同的jquery对象集中采取不同的方法:
    $('img[alt]').addClass('thickBorder').add('img[title]').addClass('');

   向jquery对象集中添加新创建的元素:
    $('p').add('

');

Delete elements from jquery object set:
$('img[title]').not('[title*=pu]')
$('img').not(function(){return !$(this).hasClass('someClassname')})
Filter jquery object set:
$('td').filter(function(){return this.innerHTML.match(^d $)}) filters td
containing numbers

Get a subset of jquery object set
$('*').slice(0,4) A new jquery object set containing the first 4 elements
$('*').slice(4) A new jquery object set containing the first 4 elements
$('div').has('img[alt]')

Convert elements in jquery object set
var allIds = $('div').map(function(){
Return (this.id==undefined) ? null : this.id;
}).get();Convert to javascript array through get method

Traverse the elements in the jquery object set
$('img').each(function(n){
This.alt = 'This is the [' n ']th picture, the id of the picture is' this.id;
})
$([1,2,3]).each(function(){alert(this);})

Use the relationship between elements to obtain a jquery object set
$(this).closest('div') For example, in which div the triggered button occurs
$(this).siblings('button[title="Close"]')All sibling elements, excluding itself
$(this).children('.someclassname')All child node elements, excluding duplicate child nodes
$(this).closest('') is adjacent to the ancestor element
$(this).contents() is a set of jquery objects composed of element contents. For example, you can get the