目录
1.一个简单的JavaScript程序,实现单击网页中的按钮改变网页的背景色。
2.验证用户输入的密码是否正确。
1.一个简单的JavaScript程序,实现单击网页中的按钮改变网页的背景色。
<script>function color(str) {document.body.style.backgroundColor = str;}</script>
<body><input type="button" value="设为红色" onclick="color('red')"><input type="button" value="设为黄色" onclick="color('yellow')"><input type="button" value="设为蓝色" onclick="color('blue')"><input type="button" value="设为自定义颜色" onclick="color('#00ff00')"></body>
运行效果:
2.验证用户输入的密码是否正确。
实现的方法是通过输入框获取用户输入的密码,然后进行判断。若输入正确,则提示“密码输入正确!”,否则提示“密码输入错误!”。
<script>var password = prompt('请输入密码:');if (password == '123456') {alert('密码输入正确!');} else {alert('密码输入错误!');}</script>
运行效果: