当前位置:Gxlcms > JavaScript > jQuery基础知识小结_jquery

jQuery基础知识小结_jquery

时间:2021-07-01 10:21:17 帮助过:17人阅读

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('

');

删除jquery对象集中的元素:
$('img[title]').not('[title*=pu]')
$('img').not(function(){return !$(this).hasClass('someClassname')})
过滤jquery对象集:
$('td').filter(function(){return this.innerHTML.match(^\d+$)})过滤包含数字的td

获取jquery对象集的子集
$('*').slice(0,4)包含前4个元素的新的jquery对象集
$('*').slice(4)包含前4个元素的新的jquery对象集
$('div').has('img[alt]')

转换jquery对象集中的元素
var allIds = $('div').map(function(){
return (this.id==undefined) ? null : this.id;
}).get();通过get方法转换成javascript数组

遍历jquery对象集中的元素
$('img').each(function(n){
this.alt = '这是第['+n+']张图片,图片的id是' + this.id;
})
$([1,2,3]).each(function(){alert(this);})

使用元素间关系获取jquery对象集
$(this).closest('div')比如触发的按钮在哪个div中发生
$(this).siblings('button[title="Close"]')所有同级元素,不包含本身
$(this).children('.someclassname')所有子节点元素,不包含重复子节点
$(this).closest('')临近祖先元素
$(this).contents()由元素内容组成的jquery对象集,比如可以获取