目录
jQuery方便了我们使用JavsScript进行DOM操作的,首先我们需要知道jQuery的官网,通过官网下载jQuery,我们主要下载的是压缩板的,因为压缩版的大小会更小一些
jQuery官网https://jquery.com/download/
当我们下载过后需要对jQuery文件进行引用,我们将jQuery文件放到使用的文件夹中
通过script标签的src属性赋值,添加jQuery文件对应的链接
基础选择器:
元素选择器:
jQuery通过$("元素名")获取对应的元素集
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<script>
var div = $("div");//相当js的document.getElementsByTagName("div")
//测试
div.css("background-color","green");
</script>
</body>
本来背景色是粉色的,我们通过元素选择器将对应的元素的背景色改为绿色(green),这里使用的css方法是对css进行操作的,我们后面会讲,这里用来测试
类选择器
jQuery通过$(".类名")获取对应类名的元素集
<head>
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
<style>
.box{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
var box = $(".box");//相当于js的document.getElementsByClassName("box")
//测试
box.css("background-color","green");
</script>
</body>
</html>
id选择器
jQuery通过$("#id名")获取对应的元素
<head>
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
<style>
#box{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
var box = $("#box");//相当于document.getElementById("box");
//测试
box.css("background-color","green");
</script>
</body>
</html>
关系选择器:
子代选择器
定义:
选择被E元素直接包含的F元素,用>表示
语法:
E > F
<head>
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul id="ul">
<li>item 1</li>
<li>
<ul>
<li>item 2.1</li>
<li>item 2.2</li>
</ul>
</li>
<li>item 3</li>
<li>item 4</li>
</ul>
<p>段落</p>
<p>段落</p>
<p>段落</p>
<script>
//子代选择器,选择#ul的直接子元素li
$("#ul>li").css("border","1px solid red");
</script>
</body>
后代选择器
定义:
选择所有被E元素包含的F元素,中间用空格隔开
语法:
E F
<head>
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul id="ul">
<li>item 1</li>
<li>
<ul>
<li>item 2.1</li>
<li>item 2.2</li>
</ul>
</li>
<li>item 3</li>
<li>item 4</li>
</ul>
<p>段落</p>
<p>段落</p>
<p>段落</p>
<script>
//后代选择器,该元素下的所有li元素
$("#ul li").css("border","1px solid red");
</script>
</body>
属性选择器:
完美匹配:
元素名[属性="value"]
选择指定属性是给定值的元素
<body>
<a href="#" alt="sxt">sxt</a>
<a href="#" alt="sxt-itbaizhan">sxt-itbaizhan</a>
<a href="#" alt="sxtitbaizhan">sxtitbaizhan</a>
<script>
//完美匹配"sxt"
$("a[alt=sxt]").css("border","1px solid red");
</script>
</body>
前缀匹配or完美匹配:
元素名[属性 |= "value"]
<body>
<a href="#" alt="sxt">sxt</a>
<a href="#" alt="sxt-itbaizhan">sxt-itbaizhan</a>
<a href="#" alt="sxtitbaizhan">sxtitbaizhan</a>
<script>
//前缀匹配or完美匹配“sxt”
$("a[alt|='sxt']").css("border","1px solid red");
</script>
</body>
包含:
元素名[属性*="value"]
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<a href="#" alt="sxt">sxt</a>
<a href="#" alt="sxtitbaizhan">sxtitbaizhan</a>
<a href="#" alt="sxt-itbaizhan">sxt-itbaizhan</a>
<script>
$("a[alt*='sxt']").css("border","1px solid red");
</script>
</body>
空格分隔or完美匹配:
元素名[属性~="value"]
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<a href="#" alt="sxt">sxt</a>
<a href="#" alt="sxt itbaizhan">sxt itbaizhan</a>
<a href="#" alt="sxt-itbaizhan">sxt-itbaizhan</a>
<script>
$("a[alt~=sxt]").css("border","1px solid red");
</script>
</body>
结尾or完美匹配:
元素名[属性$="value"]
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<a href="#" alt="sxt">sxt</a>
<a href="#" alt="itbaizhansxt">itbaizhansxt</a>
<a href="#" alt="sxt-itbaizhan">sxt-itbaizhan</a>
<script>
$("a[alt$='sxt']").css("border","1px solid red");
</script>
</body>
开头or完美匹配:
元素名[属性^=“value”]
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<a href="#" alt="sxt">sxt</a>
<a href="#" alt="itbaizhansxt">itbaizhansxt</a>
<a href="#" alt="sxt-itbaizhan">sxt-itbaizhan</a>
<script>
$("a[alt^='sxt']").css("border","1px solid red");
</script>
</body>
扩展选择器:
当我们使用基础选择器,或者属性选择器,或者关系选择器选择好元素之后,我们都可以通过冒号( : )+扩展选择器对元素再一次选择,就相当于对我们选择的元素再一次过滤,选择更加合适的元素
:eq(index)下标选择器
在匹配的集合中选择索引值为index的元素。(下标)
index下标计算是从0开始的
<body>
<ul id="ul">
<li>item 1</li>
<li>
<ul>
<li>item 2.1</li>
<li>item 2.2</li>
</ul>
</li>
<li>item 3</li>
<li>item 4</li>
</ul>
<p>段落</p>
<p>段落</p>
<p>段落</p>
<script>
$("li:eq(0)").css("border","1px solid red");
</script>
</body>
:even偶数选择器
在匹配的集合中选择下标为偶数的元素
注意:下标从0开始计算
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul>
<li>item0</li>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
<script>
//偶数选择器
$("li:even").css("border","1px solid red");
</script>
</body>
:odd奇数选择器
在匹配的集合中选择下标为奇数的元素
注意:下标从0开始计算
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul>
<li>item0</li>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
<script>
//奇数选择器
$("li:odd").css("border","1px solid red");
</script>
</body>
:first选择器
在匹配元素集中的选择第一个元素
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul>
<li>item0</li>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
<script>
//在匹配的元素集中选择第一个元素
$("li:first").css("border","1px solid red");
</script>
</body>
:last选择器
在匹配的元素集中选择最后一个元素
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul>
<li>item0</li>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
<script>
//在匹配的元素集中选择最后一个元素
$("li:last").css("border","1px solid red");
</script>
</body>
:gt(index)选择器
在匹配的集合中,选择下标大于指定下标的所有元素(index)
注意:下标从0开始计算
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul>
<li>item0</li>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
<script>
//在匹配的集合中选择大于指定下标的所有元素
$("li:gt(2)").css("border","1px solid red");
</script>
</body>
:lt(index)选择器
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul>
<li>item0</li>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
<script>
//在匹配的集合中选择小于指定下标的所有元素
$("li:lt(2)").css("border","1px solid red");
</script>
</body>
DOM操作:
Class操作:
addClass("类名")
向类中添加class属性,值得注意的是这个方法不会替换一个样式类名。它只是简单的添加一个样式类名到元素上
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div class="box"></div>
<script>
//向元素中添加box1 box2类名
$(".box").addClass("box1 box2");
</script>
</body>
removeClass("类名")
删除类中指定的类名,如果为空那么类名全部删除
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div class="box box1 box2"></div>
<div class="b b1 b2"></div>
<script>
//删除box类名
$(".box").removeClass("box");
//删除所有类名
$(".b").removeClass();
</script>
</body>
toggleClass("类名")
这是一个开关方法,如果类名存在则删除,不存在则添加
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div class="box"></div>
<script>
//存在则删除
$("div").toggleClass("box");
//不存在则添加
$("div").toggleClass("box1");
</script>
</body>
hasClass("类名")
判断元素是否包含该类名,返回布尔值
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div class="box"></div>
<script>
//输出div元素是否含有box类名
console.log($("div").hasClass("box"));
</script>
</body>
文本操作:
html():
给元素添加内容(可以识别html语句)
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div id="box">hello</div>
<div id="box1">hello</div>
<script>
//添加内容
$("#box").html("world");
//识别html语句
$("#box1").html("<p>hello world</p>");
</script>
</body>
text():
作用几乎和html()一样,但是html()可以识别html语句,但是text()无法识别html语句,直接将内容渲染为字符串添加到元素中
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div id="box">hello</div>
<div id="box1">hello</div>
<script>
//添加内容
$("#box").text("world");
//识别html语句
$("#box1").text("<p>hello world</p>");
</script>
</body>
val():
获取/设置对应元素的value值
<head>
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<input type="text" value="默认内容">
<script>
//获取val值
console.log($("input").val());
//设置val值
$("input").val("设置后的内容");
</script>
</body>
属性操作:
attr():
给元素设置/获取属性
1、一次设置一条属性
2、通过对象的形式设置多条属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
<style>
.box1{
width: 200px;
height: 200px;
background-color: pink;
}
#box1{
background-color: red;
}
</style>
</head>
<body>
<div></div>
<p></p>
<script>
//设置一条属性
$("div").attr("class","box1");
//通过对象的形式设置多条属性
$("p").attr({
class:"box1",
id:"box1"
})
//给定需要的属性,获取指定属性值
console.log($("p").attr("class"));
</script>
</body>
</html>
removeAttr():
删除元素的指定属性,无法全部删除
<head>
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<div class="box1" id="box1" style="border:1px solid red;width: 200px;height: 200px;"></div>
<script>
$(".box1").removeAttr("style");
</script>
</body>
插入元素并包含指定内容:
wrap():
创建一个元素,包裹对应元素,如果有多个对应元素,那么每个对应元素包裹一层
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<script>
//找到p元素,并包裹一层div,如果有多个那么每个包裹一层
$("p").wrap("<div class='container'></div>")
</script>
</body>

unwrap():
删除一层包裹
<head>
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<div>
<p>hello world</p>
</div>
<script>
//找到p元素,删除一层包裹
$("p").unwrap();
</script>
</body>
wrapAll():
wrap()如果有多个元素,每个都包裹一层
而wrapAll()就是如果有多个元素,所有对应元素外面包裹一层
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<script>
//找到所有p元素,在外面包裹一层div
$("p").wrapAll("<div class='container'></div>");
</script>
</body>
wrapInner():
对应元素里面的内容包裹一层
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<script>
//找到所有p元素,每个对应p元素中的内容包裹一层div
$("p").wrapInner("<div class='container'></div>");
</script>
</body>
</html>
元素内添加内容:
append():
在元素内的尾部添加内容
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
p{
background-color: pink;
}
</style>
</head>
<body>
<div>
<p>hello</p>
</div>
<script>
//在div元素内的尾部添加p标签
$("div").append("<p>world</p>");
</script>
</body>
prepend():
在元素内的头部添加内容
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
p{
background-color: pink;
}
</style>
</head>
<body>
<div>
<p>hello</p>
</div>
<script>
//在div元素内的头部添加p标签
$("div").prepend("<p>world</p>");
</script>
</body>
元素外添加内容:
after():
在对应元素的后面添加内容
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div>
<p>hello</p>
</div>
<script>
//在对应元素后面添加p标签
$("div").after("<p>world</p>");
</script>
</body>
before():
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div>
<p>hello</p>
</div>
<script>
//在对应元素的前面添加内容
$("div").before("<p>world</p>");
</script>
</body>
</html>
移除:
empty():
删除元素中的内容
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div>
<p>hello</p>
<p>world</p>
<p>你好</p>
</div>
<script>
//移除div里边的内容
$("div").empty();
</script>
</body>
remove():
删除整个元素,包括里面的内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div>
<p>hello</p>
<p>world</p>
<p>你好</p>
</div>
<script>
//移除div
$("div").remove();
</script>
</body>
</html>
替换:
replacWith():
匹配对应的元素并替换为对应的元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div>
<p>hello</p>
<p>world</p>
</div>
<script>
//将对应元素替换为指定元素
$("p").replaceWith("<b>替换后的内容</b>");
</script>
</body>
</html>
CSS操作:
css方法:
css方法和attr方法很像,但是css方法只能设置属性值
1、一次设置一个属性
2、通过对象的方式设置多个属性
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div id="box1"></div>
<div id="box2"></div>
<script>
//一次设置一个属性
$("#box1").css("border","1px solid red");
$("#box1").css("width","100px");
$("#box1").css("height","100px");
//通过对象的方式设置多个属性
$("#box2").css({
width:200,
height:200,
backgroundColor:"pink"
})
</script>
</body>
</html>
height()和width()
获取对应元素的高度和宽度,不包括padding、边框、margin
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
padding: 10px;
border: 5px;
margin: 10px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<script>
//获取div的高度和宽度,不包括padding、边框、margin
var width = $("div").width();
var height = $("div").height();
console.log("height:"+height);
console.log("width:"+width);
</script>
</body>
</html>
innerHeight()和innerWidth()
获取对应元素的高度和宽度,包括padding内边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
padding: 10px;
border: 5px;
margin: 10px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<script>
//获取div的高度和宽度,包括padding 不包括边框、margin
var iwidth = $("div").innerWidth();
var iheight = $("div").innerHeight();
console.log("innerheight:"+iheight + " (包括padding)");
console.log("innerwidth:"+iwidth + " (包括padding)");
</script>
</body>
</html>
outerHeight()和outerWidth()
获取对应元素的高度和宽度,包括padding、border,默认不包括margin,可以给定参数true,使包括margin
不包括margin:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
padding: 10px;
border: 5px solid red;
margin: 10px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<script>
//获取div的高度和宽度,包括padding、边框,默认不包括margin
var owidth = $("div").outerWidth();
var oheight = $("div").outerHeight();
console.log("outerheight:"+oheight + " (包括padding+border)");
console.log("outerwidth:"+owidth + " (包括padding+border)");
</script>
</body>
</html>
包括margin:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
padding: 10px;
border: 5px solid red;
margin: 10px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<script>
//获取div的高度和宽度,包括padding、边框,默认不包括margin
var owidth = $("div").outerWidth(true);
var oheight = $("div").outerHeight(true);
console.log("outerheight:"+oheight + " (包括padding+border+marin)");
console.log("outerwidth:"+owidth + " (包括padding+border+margin)");
</script>
</body>
</html>
offset()
获取元素与文档的距离
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
#box1{
width: 200px;
height: 200px;
background-color: pink;
margin: 50px;
}
#box2{
width: 100px;
height: 100px;
background-color: green;
}
</style>
</head>
<body>
<div id="box1">
<div id="box2"></div>
</div>
<script>
//获取box2与文档的距离
console.log($("#box2").offset());
</script>
</body>
</html>
position()
获取元素与父类具有定位功能的距离
如果父类没有定位功能:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
#box1{
width: 200px;
height: 200px;
margin: 50px;
background-color: pink;
}
#box2{
width: 100px;
height: 100px;
background-color: green;
}
</style>
</head>
<body>
<div id="box1">
<div id="box2"></div>
</div>
<script>
//获取box2与父类具有定位功能的距离
console.log($("#box2").position());
</script>
</body>
</html>
父类具有定位功能:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
#box1{
width: 200px;
height: 200px;
margin: 50px;
position: relative;
background-color: pink;
}
#box2{
width: 100px;
height: 100px;
background-color: green;
}
</style>
</head>
<body>
<div id="box1">
<div id="box2"></div>
</div>
<script>
//获取box2与父类具有定位功能的距离
console.log($("#box2").position());
</script>
</body>
</html>
事件:
on事件处理器
需要两个参数,第一个参数是事件类型,第二个参数是触发事件后的行为
语法:
on(事件类型,触发事件后的行为)
测试:这里我们就先使用click点击事件作为测试,click事件后面会详细讲解
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<button id="btn">按钮</button>
<script>
$("#btn").on("click",
function(){
console.log("点击了按钮");
})
</script>
</body>
</html>
one一次性事件处理器
one事件处理器的使用方法和on事件处理器一样,但是one事件处理器只会被触发一次
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<button id="btn">按钮</button>
<script>
$("#btn").one("click",
function(){
console.log("一次性事件");
})
</script>
</body>
</html>
off清除事件处理器
off删除对应元素的事件,可以传递参数,如果不传递参数那么清除所有事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<button id="btn">按钮</button>
<script>
$("#btn").on("click",
function(){
console.log("点击了按钮");
});
$("#btn").on("mouseenter",
function(){
console.log("进入了按钮");
});
//清除所有事件
$("#btn").off();
</script>
</body>
</html>
鼠标事件:
click():
点击事件,当鼠标点击拥有该事件的元素就会触发
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<button id="btn">按钮</button>
<script>
$("#btn").click(function(){
console.log("点击了按钮");
});
//相当于$("#btn").on("click",function(){
// console.log("点击了按钮");
// })
</script>
</body>
</html>
mouseenter()和mouseleave():
mouseenter():当鼠标进入触发(不会事件冒泡)
mouseleave():当鼠标离开触发(不会事件冒泡)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
<style>
#box{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
$("#box").mouseenter(function(){
console.log("鼠标进入了");
});
$("#box").mouseleave(function(){
console.log("鼠标离开了");
})
</script>
</body>
</html>
mouseover()和mouseout():
mouseover():鼠标进入触发(会事件冒泡)
mouseout():鼠标离开触发(会事件冒泡)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
<style>
#box{
width: 200px;
height: 200px;
background-color: pink;
}
#box2{
width: 100px;
height: 100px;
background-color: green;
}
</style>
</head>
<body>
<div id="box">
<div id="box2"></div>
</div>
<script>
$("#box").mouseover(function(){
console.log("鼠标进入了box");
});
$("#box").mouseout(function(){
console.log("鼠标离开了box");
})
$("#box2").mouseover(function(){
console.log("鼠标进入了box2");
});
$("#box2").mouseout(function(){
console.log("鼠标离开了box2");
})
</script>
</body>
</html>
mousemove():
鼠标移动触发该事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
<style>
#box{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
$("#box").mousemove(function(){
console.log("鼠标移动了");
});
</script>
</body>
</html>
表单事件:
focus():
获得焦点时触发该事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<input type="text" id="username">
<script>
$("#username").focus(function(){
console.log("获得焦点");
})
</script>
</body>
</html>
blur():
失去焦点时触发事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<input type="text" id="username">
<script>
$("#username").focus(function(){
//获得焦点时触发事件
console.log("获得焦点");
})
$("#username").blur(function(){
//失去焦点时打印输入框内容
console.log($("#username").val());
})
</script>
</body>
</html>
change():
输入框内容改变过后失去焦点或输入回车触发该事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<input type="text" id="username">
<script>
$("#username").change(function(){
//当触发该事件时输出输入框中的内容
console.log($("#username").val());
})
</script>
</body>
</html>
submit():
submit事件只能给form表单设置,触发该事件将会提交数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<form action="">
username:<input type="text" id="username"><br>
password:<input type="password" id="pwd">
<input type="submit" id="sub" value="提交">
</form>
<script>
$("#sub").click(function(){
//当点击提交按钮后,触发form表单的submit事件
$("form").submit(function(){
alert("提交了数据");
})
})
</script>
</body>
</html>
键盘事件:
keydown():
当键盘按下触发该事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<input type="text" id="username">
<script>
$("#username").keydown(function(){
console.log("键盘按下");
})
</script>
</body>
</html>
keyup():
键盘抬起触发该事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<input type="text" id="username">
<script>
$("#username").keyup(function(){
console.log("键盘抬起");
})
</script>
</body>
</html>
keypress():
键盘按下触发
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
</head>
<body>
<input type="text" id="username">
<script>
$("#username").keypress(function(){
console.log("触发keypress");
})
</script>
</body>
</html>
浏览器事件:
resize():
当页面两端大小改变时,触发该事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
<style>
#box1{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div id="box1"></div>
<script>
var box1 = $("#box1")
$(window).resize(
//当页面两端大小发生改变时,触发该事件
function(){
if($(window).width() > 800){
//当页面两端大小>800px是,box1显示
box1.css("display","block");
}else{
//否则消失
box1.css("display","none");
}
}
)
</script>
</body>
</html>
scroll():
当页面滚动时触发该事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../jquery-3.6.1.min.js"></script>
<style>
h3{
height: 500px;
}
</style>
</head>
<body>
<h3>标题</h3>
<h3>标题</h3>
<h3>标题</h3>
<h3>标题</h3>
<h3>标题</h3>
<script>
$(window).scroll(
function(){
console.log($(window).scrollTop());
}
)
</script>
</body>
</html>
Event对象的属性
target:
target谁触发了该事件指向谁
currentTarget:
currentTarget当事件冒泡到谁身上就指向谁
<html>
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
#box1 {
width: 500px;
height: 500px;
background-color: pink;
}
#box2 {
width: 100px;
height: 100px;
background-color: green;
}
</style>
</head>
<body>
<div id="box1">
<div id="box2">
<button id="btn">按钮</button>
</div>
</div>
<script>
//e.target谁触发了该事件指向谁
//e.currentTarget当事件冒泡到谁身上就指向谁
$("#box1").click(
function (e) {
console.log(e.target);
console.log(e.currentTarget);
}
)
$("#box2").click(
function (e) {
console.log(e.target);
console.log(e.currentTarget);
}
)
$("#btn").click(
function (e) {
console.log(e.target);
console.log(e.currentTarget);
}
)
</script>
</body>
</html>
Event对象的方法
preventDefault():
阻止浏览器默认行为
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<a href="https://www.baidu.com">百度</a>
<script>
$("a").click(
function(e){
//停止浏览器默认事件
e.preventDefault();
console.log("点击了,但我不跳咋滴");
}
)
</script>
</body>
</html>
stopPropagation():
阻止事件冒泡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
#box1{
width: 200px;
height: 200px;
background-color: pink;
}
#box2{
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div id="box1">
<div id="box2"></div>
</div>
<script>
$("#box2").click(
//当点击子级box2,不再事件冒泡到父级box1
function(e){
e.stopPropagation();
console.log("点击了box2");
}
)
$("#box1").click(
function(){
console.log("点击了box1");
}
)
</script>
</body>
</html>
遍历:
map()和each()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
<script>
$("li").map(
//第一个参数:代表每个元素的下标(index)
//第二个参数:代表每个元素
function(index,ele){
//map方法和each方法获得到的都是js对象
console.log(ele);
}
);
$("li").each(
//第一个参数:代表每个元素的下标(index)
//第二个参数:代表每个元素
function(index,ele){
//map方法和each方法获得到的都是js对象
console.log(ele);
}
);
</script>
</body>
</html>
js对象和jQuery对象的互转:
为什么js对象和jQuery对象需要互转?
因为js对象和jQuery对象的方法不能互相使用
js对象 - > jQuery对象($(js对象))
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<div></div>
<script>
//js -> jQuery
console.log($(document.getElementsByTagName("div")[0]));
</script>
</body>
</html>
jQuery对象 - > js对象
1、通过get(),获取一个js对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
</ul>
<script>
//输出第一个对应的li元素(js对象)
console.log($("li").get(0));
</script>
</body>
</html>
2、通过map()、each()获取多个js对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
</head>
<body>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
</ul>
<script>
$("li").map(
//第一个参数:代表每个元素的下标(index)
//第二个参数:代表每个元素
function(index,ele){
//map方法和each方法获得到的都是js对象
console.log(ele);
}
);
$("li").each(
//第一个参数:代表每个元素的下标(index)
//第二个参数:代表每个元素
function(index,ele){
//map方法和each方法获得到的都是js对象
console.log(ele);
}
);
</script>
</body>
</html>
动画:
show()和hide()
.show()
执行显示动画
.hide()
执行隐藏动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<button id="show">show</button>
<button id="hide">hide</button>
<script>
var show = $("#show");
var hide = $("#hide");
var div = $("div");
show.click(
function(){
//动画一秒后显示div
div.show(1000);
}
)
hide.click(
function(){
//动画一秒隐藏div
div.hide(1000);
}
)
</script>
</body>
</html>
fadein()和fadeout()
.fadeIn()
通过淡入的方式显示匹配元素。
.fadeOut()
通过淡出的方式显示匹配元素
<head>
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<button id="fadein">淡入</button>
<button id="fadeout">淡出</button>
<script>
var fadein = $("#fadein");
var fadeout = $("#fadeout")
fadein.click(
function(){
$("div").fadeIn(2000);
}
)
fadeout.click(
function(){
$("div").fadeOut(2000);
}
)
</script>
</body>
slideDown()和slideUp()
.slideDown()
用滑动动画显示一个元素
.slideUp()
用滑动动画隐藏一个元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.6.1.min.js"></script>
<style>
div{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<button id="up">up</button>
<button id="down">down</button>
<script>
var show = $("#up");
var hide = $("#down");
var div = $("div");
show.click(
function(){
//动画一秒后划出div
div.slideDown(1000);
}
)
hide.click(
function(){
//动画一秒隐藏div
div.slideUp(1000);
}
)
</script>
</body>
</html>
自定义动画animate()
自定义动画
语法:
选择器.animate(
{
动画所执行的操作
}, 执行时间);
);