id选择器
基础语法:$(selector).action()
美元符号:定义jQuery
选择符(selector)查询和查找HTML元素
action()执行对元素的操作
案例:点击按钮,改变div的宽高
<head>
<style type="text/css">
#div1{
width:100px;
height:100px;
background-color:red;
}
</style>
<script type="text/javascript" src="./js/jquery-3.3.1.js"><script>
<script type="text/javascript">
$(function(){
//给按钮添加点击事件
$('#btn').click(function(){
//更新div的宽高
$('#div1').width(200);
$('#div1').height(200);
});
});
</script>
</head>
<body>
<div id="div1"></div>
<button id="btn">点击</button>
</body>
类选择器
<head>
<style type="text/css">
.mydiv1{
width:100px;
height:100px;
background-color:red;
}
</style>
<script type="text/javascript" src="./js/jquery-3.3.1.js"><script>
<script type="text/javascript">
$(function(){
//给按钮添加点击事件
$('.btn').click(function(){
//更新div的宽高
$('.mydiv1').width(200);
$('.mydiv1').height(200);
});
});
</script>
</head>
<body>
<div class="mydiv1"></div>
<button class="btn">点击</button>
</body>
element元素选择器
<head>
<style type="text/css">
#div1{
width:100px;
height:100px;
background-color:red;
}
#div2{
width:100px;
height:100px;
background-color:blue;
}
</style>
<script type="text/javascript" src="./js/jquery-3.3.1.js"><script>
<script type="text/javascript">
$(function(){
//给按钮添加点击事件
$('#btn').click(function(){
//元素选择器,同时更改两个div的宽高
//更新div的宽高
$('div').width(200);
$('div').height(200);
});
});
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<button id="btn">点击</button>
</body>
jQuery的事件监听函数
|函数|绑定函数至 |
|
(
d
o
c
u
m
e
n
t
)
.
r
e
a
d
y
(
f
u
n
c
t
i
o
n
)
∣
将
函
数
绑
定
到
问
文
档
的
就
绪
事
件
(
当
文
档
完
成
加
载
时
)
∣
∣
(document).ready(function)|将函数绑定到问文档的就绪事件(当文档完成加载时)| |
(document).ready(function)∣将函数绑定到问文档的就绪事件(当文档完成加载时)∣∣(document).click(function)|触发或将函数绑定到被选元素的点击事件中 |
|
(
d
o
c
u
m
e
n
t
)
.
d
b
l
c
l
i
c
k
(
f
u
n
c
t
i
o
n
)
∣
触
发
或
将
函
数
绑
定
到
被
选
元
素
的
双
击
事
件
中
∣
∣
(document).dblclick(function)|触发或将函数绑定到被选元素的双击事件中| |
(document).dblclick(function)∣触发或将函数绑定到被选元素的双击事件中∣∣(document).focus(function) | 触发或将函数绑定到被选元素的获得焦点事件|
|$(document).mouseover(function)|触发或将函数绑定到被选元素的鼠标悬停事件 |
组合选择器
组合选择器也就是将id,元素,类选择器组合使用
<script type="text/javascript" src="./js/jquery-3.3.1.js"><script>
<script type="text/javascript">
$(function(){
/*元素选择器+id选择器*/
//查找所有span标签中id为s1的,将其背景设置为红色
$('span#s1').css('background-color','red');
/*元素选择器+类选择器*/
$('span.s2').css('background-color'.'yellow');
/*元素选择器+元素选择器*/
//找到所有div标签下的p标签,将其背景设置为灰色
$('div p').css('background-color'.'gray');
/*元素选择器+元素选择器+类选择器*/
//找到div标签下的p标签中class为p2的,将其背景设置为黑色
$('div p.p2').css('background-color'.'black');
});
</script>
属性选择器
jQuery使用XPath表达式来选择带有给定属性的元素
$(’[src]’)选取所有带有href属性的元素
$(’[src="#"]’)选取所有带有href值等于#的元素
(
′
[
s
r
c
('[src
(′[src=".jpg"]’)选取所有href值以.jpg结尾的元素
<head>
<script type="text/javascript" src="./js/jquery-3.3.1.js"><script>
<script type="text/javascript">
$(function(){
$('[src]').css("border-style","solid");//所有的元素中(不光只要图片,例如div中设置图片背景是用src指定图片路径)只要有属性为src的都添加实线边框
$('[src="./image/002.jpg"]').css("border-style","solid");//给标签src属性符合字符串路径的添加实线边框
$('[src$=".png"]').css("border-style","solid");//给标签中元素src路径中以png结尾的添加实线边框
});
<head>
<body>
<img alt="" src="./image/001.jpg" width="100" height="100"/>
<img alt="" src="./image/002.jpg" width="100" height="100"/>
<img alt="" src="./image/003.png" width="100" height="100"/>
</body>