目录
jQuery对象就是通过jQuery包装DOM对象后产生的对象。
var $variable = jQuery对象
$variable[0] // jQuery对象转成DOM对象
$('#id') // id选择器
$('tagName') // 标签选择器
$('.cl) // class选择器
$('div.cl') // 找到有cl类的div标签
$('#id, .className, tagName') // 组合选择器
$('x y')
$('x > y')
$('x + y') // 毗邻选择器
$('x ~ y') // 兄弟选择器
:first
:last
:eq(index)
:even
:odd
:gt(index)
:lt(index)
:not(元素选择器)
:has(元素选择器)
$('div:has(h1)')
[attr]
[attr=value]
[attr!=value]
$('input[type!="text"]')
根据type类型
:text
:password
:file
:radio
:checkbox
:submit
:reset
:button
根据对象属性
:enabled
:disabled
:checked
:selected
下一个元素
$('#id').next()
$('#id').nextAll()
$('#id').nextUntil('#i2')
上一个元素
$('#id').prev()
$('#id').prevAll()
$('#id').prevUntil('#i2')
父亲元素
$('#id').parent()
$('#id').parents()
$('#id').parentsUntil()
儿子和兄弟元素
$('#id').children()
$('#id').siblings()
后代元素
$('div').find('p')
$('div').filter('.c1')
.first()
.last()
.not()
.has()
.eq()
addClass()
removeClass()
hasClass()
toggleClass()
tag.css('color', 'red') // tag.style.color = 'red'
offset()
position()
scrollTop()
scrollLeft()
height()
width()
innerHeight()
innerWidth()
outerHeigth()
outerWidth()
HTML
html()
html(val) // 设置元素html内容
文本
text()
值
val()
$('[name='hobby']').val(['basketball', 'football'])
attr(attrName)
attr(attrName, attrValue)
attr({k1:v1, k2:v2})
removeAttr()
对于checkbox和radio
prop()
注意:
append()
appendTo()
prepend()
prependTo()
.afer()
.insertAfer()
.before()
.insertBefore()
移除和清空元素
remove()
empty()
替换
replaceWith()
replaceAll()
克隆
clone()
click(func(){..})
hover
blur
focus
change
keyup
.on(event, [, selector], function(){})
.off(event, [, selector], function(){})
1. return false;
2. 2.preventDefault()
$('span').click(function(e)) {
alert('span');
e.stopPropagation();
}
$(document).read(function(){..})
$(function(){...})
$('table').on('click', '.delete', function(){})
原文:https://www.cnblogs.com/YajunRan/p/11894441.html