<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../js/jquery-3.5.1.js"></script>
<script>
$(function () {
//属性操作
//设置属性,$().attr("属性","取值");
var flag = true;
$("#btn1").click(function () {
if(flag)
{
$("#pic").attr("src","img/2.png")
.attr("alt","2.png");
flag = false;
}
else
{
$("#pic").attr("src","img/1.png")
.attr("alt","1.png");
flag = true;
}
});
});
/*
在jQuery,设置属性有两种方式,
一种是,设置一个属性,另外一个是设置多个属性
语法:
$().attr("属性","取值");
$().attr({"属性1":"取值1","属性2":"取值2",......});
*/
</script>
</head>
<body>
<input type="button" id="btn1" value="修改"><br>
<img id="pic" src="img/1.png" alt="1.png">
</body>
</html>