随机产生验证码

随机产生验证码

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>随机产生验证码</title>
	<style type="text/css">
		#code{
			width: 145px;
			height: 50px;
			background-color: lightblue;
			font-size: 44px;
			letter-spacing: 5px;
			text-align: center;
		}
		#container{
			width: 145px;
			height: 80px;
			background-color: lightblue;
			text-align: center;
		}
	</style>
</head>
<body>
	<script type="text/javascript">
		function createCode(){
			var chars=['Q','D','L','1','2','3'];
			var randCode="";
			for(var i=0;i<4;i++)//产生4位的随机码
			{
				var pos=parseInt(Math.random()*(chars.length -1));
				randCode+=chars[pos];
			}
			document.getElementById("code").innerHTML=randCode;
		}
	</script>
	<div id="container">
		<div id="code"></div>
		<button onclick="createCode()">验证码</button>
	</div>

</body>
</html>

猜数字

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>循环结构</title>
</head>
<body>
	<script type="text/javascript">
		var num,ans;
		ans=parseInt(Math.random()*100);
		do{
			num=prompt("请输入代猜数字.");
			if(num==ans)
			{
				alert("correct answer!");
			}
			else if(num>ans)
			{
				alert("bigger than answer!");
			}
			else alert("smaller than answer!");
		}
		while(ans!=num);
	</script>
</body>
</html>
发布了100 篇原创文章 · 获赞 56 · 访问量 4854

猜你喜欢

转载自blog.csdn.net/weixin_44307065/article/details/103986930
今日推荐