选择器
基本选择器
$("#id")
$(".className")
$("tagName")
$("#id, .className, tagName")
$("div.c1")
层级选择器:
$("x y");
$("x > y");
$("x + y")
$("x ~ y")
属性选择器
[attribute]
[attribute=value]
[attribute!=value]
<input type="text">
<input type="password">
<input type="checkbox">
$("input[type='checkbox']");
$("input[type!='text']");
基本筛选器:
:first
:last
:eq(index)
:gt(index)
:lt(index)
:even
:odd
:not(元素选择器)
:has(元素选择器)
示例
$("div:has(h1)")
$("div:has(.c1)")
$("li:not(.c1)")
$("li:not(:has(a))")
注意:
这里的has和not不是简单的有和没有的意思,它俩没啥关系(不是一组)
:not和:has通常用.not()和.has()代替。
$("div:has(.c1)")等价于$("div .c1")并不等价于$("div.c1")。
筛选器
下一个元素:
$("#id").next()
$("#id").nextAll()
$("#id").nextUntil("#i2")
上一个元素:
$("#id").prev()
$("#id").prevAll()
$("#id").prevUntil("#i2")
父亲元素:
$("#id").parent()
$("#id").parents()
$("#id").parentsUntil()
儿子和兄弟元素:
$("#id").children();
$("#id").siblings();
查找元素:
$("id").find()
补充:
.first()
.last()
.not()
.has()
表单筛选器
:text
:password
:file
:radio
:checkbox
:submit
:reset
:button
示例
$(":checkbox")
表单对象属性
:enabled
:disabled
:checked
:selected
示例
<form>
<input name="email" disabled="disabled" />
<input name="id" />
</form>
$("input:enabled") // 找到可用的input标签
<select id="s1">
<option value="beijing">北京市</option>
<option value="shanghai">上海市</option>
<option selected value="guangzhou">广州市</option>
<option value="shenzhen">深圳市</option>
</select>
$(":selected") // 找到所有被选中的option
//或者如下
$("option:selected")
样式操作
样式类
addClass();
removeClass();
hasClass();
toggleClass();
CSS
css("color", "red") //DOM操作:tag.style.color="red"
位置
offset()
position()
scrollTop()
scrollLeft()
.offset()方法允许我们检索一个元素相对于文档(document)的当前位置。
和.position()的差别在于:.position()是相对于相对于父级元素的位移。
示例代码如下:
<script>
$("#b1").on("click", function () {
$(".c1").offset({left: 200, top:200});
});
$(window).on("scroll", function () {
if ($(window).scrollTop() > 100) {
$("#b2").removeClass("hide");
}else {
$("#b2").addClass("hide");
}
});
$("#b2").on("click", function () {
$(window).scrollTop(0);
})
</script>
尺寸
height() 内容
width()
innerHeight() 内容+内填充
innerWidth()
outerHeight() 内容+内填充+边框
outerWidth()
文本操作
HTML代码
html()
html(val)
文本值
text()
text(val)
值
val()
val(val)
val([val1, val2])
示例
<label for="s1">城市</label>
<select id="s1">
<option value="beijing">北京市</option>
<option value="shanghai">上海市</option>
<option selected value="guangzhou">广州市</option>
<option value="shenzhen">深圳市</option>
</select>
<hr>
<label for="s2">爱好</label>
<select id="s2" multiple="multiple">
<option value="basketball" selected>篮球</option>
<option value="football">足球</option>
<option value="doublecolorball" selected>双色球</option>
</select>
<hr>
<p>爱好</p>
<label>
<input type="checkbox" name="hobby" value="basketball"/> 篮球
<input type="checkbox" name="hobby" value="football"/> 足球
<input type="checkbox" name="hobby" value="doublecolorball"/> 双色球
</label>
<p>性别</p>
<label for="radio1">女</label>
<input id="radio1" type="radio" name="gender" value="female"/>
<label for="radio2">男</label>
<input id="radio2" type="radio" name="gender" value="male"/>
<script src="jquery-3.2.1.min.js"></script>
<script>
$("#s1").val("shanghai");
$("#s2").val(["basketball", "football"]);
$("input[name='hobby']").val(["basketball", "football"]);
$("input[name='gender']").val(["female"]);
</script>
属性操作
用于自定义属性
attr(attrName)
attr(attrName, attrValue)
attr({k1: v1, k2:v2})
removeAttr()
示例:
<body>
<div id="d1" egon="nb">div</div>
<input type="checkbox" value="1">1
<input type="checkbox" value="2">2
<input type="radio" name="gender" value="11">11
<input type="radio" name="gender" value="22">22
<script src="jquery-3.2.1.min.js"></script>
</body>

用于checkbox和radio
prop()
removeProp()

在1.x及2.x版本的jQuery中使用attr对checkbox进行复制操作时会出bug,在3.x版本的jQuery中则没有这个问题。为了兼容性,我们在处理checkbox和radio的时候尽量使用特定的prop(),不要使用attr(“checked”, “checked”)。
文档处理
添加到指定元素内部的后面
$(A).append(B)
$(A).appendTo(B)
添加到指定元素内部的前面
$(A).prepend(B)
$(A).prependTo(B)
添加到指定元素外部的后面
$(A).after(B)
$(A).insertAfter(B)
添加到指定元素外部的前面
$(A).before(B)
$(A).insertBefore(B)
移除和清空元素
remove()
empty()
替换
replaceWith()
replaceAll()
克隆
clone()
克隆示例代码如下:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>克隆</title>
<style>
#b1 {
background-color: deeppink;
padding: 5px;
color: white;
margin: 5px;
}
#b2 {
background-color: dodgerblue;
padding: 5px;
color: white;
margin: 5px;
}
</style>
</head>
<body>
<button id="b1">屠龙宝刀,点击就送</button>
<hr>
<button id="b2">屠龙宝刀,点击就送</button>
<script src="jquery-3.2.1.min.js"></script>
<script>
$("#b1").on("click", function () {
$(this).clone().insertAfter(this);
});
$("#b2").on("click", function () {
$(this).clone(true).insertAfter(this);
});
</script>
</body>
</html>
点击复制按钮
页面载入
当DOM载入就绪可以查询及操纵时绑定一个要执行的函数。这是事件模块中最重要的一个函数,因为它可以极大地提高web应用程序的响应速度。
两种写法:
$(document).ready(function(){
// 在这里写你的JS代码...
})
$(function(){
// 你在这里写你的代码
})