jquery怎么改变img的src属性
时间:2022-03-03 15:41
jquery改变img src属性的方法:1、使用attr()属性,语法“$("img").attr("src","图片文件的地址")”;2、使用prop()方法,语法“$("img").prop("src","图片文件的地址")”。 本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。 jquery怎么改变img的src属性 方法1:使用attr()属性 attr() 方法设置或返回被选元素的属性值。 方法2:使用prop()方法 prop() 方法设置或返回被选元素的属性和值。 【推荐学习:jQuery视频教程、web前端入门视频】 以上就是jquery怎么改变img的src属性的详细内容,更多请关注gxlsystem.com其它相关文章!<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function() {
$("img").attr("src","img/2.jpg");
});
});
</script>
</head>
<body>
<img id="img" src="img/1.jpg" width="300"/><br><br><br>
<button>更改img src属性值(换图)</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function() {
$("img").prop("src","img/3.jpg");
});
});
</script>
</head>
<body>
<img id="img" src="img/1.jpg" width="300"/><br><br><br>
<button>更改img src属性值(换图)</button>
</body>
</html>