一、操作元素
Ⅰ、操作文本内容
①语法text()
②替换文本内容
③以纯文本方式实现, 不能是标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery操作文本内容</title>
<script type="text/javascript" src="js/jQuery3.6.0.js"></script>
</head>
<body>
<div>
<p> hello </p>
</div>
<div>
<p> world </p>
</div>
<script>
$(function(){
// text()
console.log($("div").text());
alert($("div").text());
// 替换内容
$("div").text("name");
// 以纯文本实现,不能识别标签
$("div").text("<span> name </span>");
})
</script>
</body>
</html>
Ⅱ、操作HTML内容
①只匹配第一个文本内容
②可以识别标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery操作文本内容</title>
<script type="text/javascript" src="js/jQuery3.6.0.js"></script>
</head>
<body>
<div>
<p> hello </p>
</div>
<div>
<p> world </p>
</div>
<script>
// html() 匹配第一个
$(function(){
console.log($("div").html());
$("div").html("<span> name </span>")
})
</script>
</body>
</html>
Ⅲ、操作元素值
①val()
②获取第一个匹配的元素,也可修改值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery操作元素值</title>
<script type="text/javascript" src="js/jQuery3.6.0.js"></script>
</head>
<body>
<input type="text" value="123">
<input type="text" value="456">
<script>
$(function(){
// val() 获取第一个匹配的元素
alert($("input").val());
$("input").val("value");
})
</script>
</body>
</html>
Ⅳ、操作CSS
1、修改类
①addClass() 添加
②removeClass() 删除
③toggleClass() 如果指定的类存在,就删掉, 不存在就添加
2、修改CSS属性
①css(key, value)
修改多个属性时,用字典方式
②获取属性值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery操作CSS</title>
<script type="text/javascript" src="js/jQuery3.6.0.js"></script>
<style>
.box{
}
.c1{
width:100px;
height: 100px;
background-color: red;
}
.c2{
width: 100px;
height: 100px;
background-color: blue;
}
div{
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
// 修改类
// addClass()
$(function(){
$("div").addClass("c1");
})
// removeClass()
$(function(){
$("div").removeClass("c1");
})
// toggleClass()
// 如果指定的类存在,就删掉,如果不存在就添加
$(function(){
$("div").toggleClass("c2");
$("div").toggleClass("c1");
})
// 修改CSS属性
// css(key, value)
// css(value)
$(function(){
// $("div").css("background-color", "blue");
$("div").css({"background-color": "red" , "border": "1px solid black"});
// 获取属性值
alert($("div").css("width"));
})
</script>
</body>
</html>
Ⅴ、操作DOM
1、增 :在元素内部-- 创建、插入, 在元素外部--添加
2、删 : remove()
3、复制 : clone()
4、替换 : replaceAll()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery操作DOM</title>
<script type="text/javascript" src="js/jQuery3.6.0.js"></script>
</head>
<body>
<div></div>
<p></p>
<div id="div1"></div>
<div id="div2"></div>
<script>
$(function(){
// 创建节点
var $jqObj1 = $("<p>111</p>");
$("div").append($jqObj1);
var $jqObj2 = $("<p> hello </p>");
$("div").append($jqObj2);
var $jqObj3 = $("<p name='abc'> world </p>");
$("div").append($jqObj3);
// 插入 append(xx) appendTo() 调用对象不同
var $p_1 = $("<p> hello </p>");
var $p_2 = $("<p> world </p>");
var $div = $("div");
$div.append($p_1);
$p_2.appendTo($div);
})
$(function(){
// 在元素外添加
var $p = $("p");
var $p_1 = $("<p> 1 </p>");
var $p_2 = $("<p> 2 </p>");
$p.after($p_1);
$p_2.insertAfter($p);
})
$(function(){
// 删除 remove()
var $p = $("p");
$p.remove();
})
$(function(){
// 复制 clone()
var $p = $("p");
$p.clone();
})
$(function(){
// 替换 replaceAll()
$("<div> name </div>").replaceAll("#div2");
$("#div1").replaceWith("<div> hello </div>");
})
</script>
</body>
</html>
5、遍历
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery操作DOM</title>
<script type="text/javascript" src="js/jQuery3.6.0.js"></script>
</head>
<body>
<img src="img/1477550681.jpg">
<img src="img/1477550681%20-%20副本%20(2).jpg">
<img src="img/1477550681%20-%20副本%20(3).jpg">
<img src="img/1477550681%20-%20副本%20(4).jpg">
<img src="img/1477550681%20-%20副本%20(5).jpg">
<script>
$(function(){
// 遍历图片张数
$("img").each(function (index) {
$(this).attr("title", "第" + index + "张");
})
})
</script>
</body>
</html>
6、包裹
①包裹节点 wrap()
②去除包裹 unwrap()
③整体包裹 wrapAll()
④包裹内部 wrapInner()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery操作DOM</title>
<script type="text/javascript" src="js/jQuery3.6.0.js"></script>
</head>
<body>
<span> this is a good day </span> <br/>
<span> this is a bad day </span> <br/>
<span> this is a nice day </span> <br/>
<span> this is a wind day </span> <br/>
<button> 包裹节点 </button>
<button> 去除包裹 </button>
<button> 整体节点 </button>
<button> 包裹内部 </button>
<script>
$(function(){
// 包裹节点 wrap()
$("button:eq(0)").click(function(){
$("span").wrap("<p></p>");
})
// 去除包裹
$("button:eq(1)").click(function(){
$("span").unwrap();
})
// 整体包裹
$("button:eq(2)").click(function(){
$("span").wrapAll("<p></p>");
})
// 包裹内部
$("button:eq(3)").click(function(){
$("span").wrapInner("<p></p>");
})
})
</script>
</body>
</html>