18. jQuery-使用function()函数随机展示图片

版权声明:本文为大都督作者的原创文章,未经 大都督 允许也可以转载,但请注明出处,谢谢! 共勉! https://blog.csdn.net/qq_37335220/article/details/84888818

1. 效果图

在这里插入图片描述

2. html代码

<!DOCTYPE html>
<html>
<head>
    <title>jQuery-使用function()函数随机展示图片</title>
        <style type="text/css">
           body{font-size:12px}
           .clsSpn{float:left;padding-top:10px;padding-left:10px}
           .clsImg{border:solid 1px #ccc;padding:3px;float:left}
    	</style>
</head>
<body>
      <img alt="" style="float:left" width="70px;" height="70px;" src="../img/img03.jpg"/>
      
<script src="../jquery.min.js"></script>
<script type="text/javascript">
	$(function(){
		window.setInterval(refreshImg, 1000);

		//设置title属性
		$("img").attr("title", "这是一只会变身的小猪");

		//增加样式
		$("img").addClass("clsImg");
	})
	
	function refreshImg() {
		//设置src属性
		$("img").attr("src", function(){
			console.log(Math.floor(Math.random()*2+1));
			return "../img/img0"+Math.floor(Math.random()*2+1)+".jpg";
		});
	}
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_37335220/article/details/84888818
18.