H5 点击图片弹出密码输入框,输入正确跳转
时间: 2023-10-08 18:12:48 浏览: 143
可以通过HTML、CSS和JavaScript实现这个功能。
首先,需要在HTML中添加一个图片和一个密码输入框,以及一个按钮:
```html
<img src="your_image_path" id="myImage">
<input type="password" id="myPassword">
<button onclick="checkPassword()">Submit</button>
```
然后,在CSS中设置密码输入框和按钮的样式:
```css
#myPassword {
display: none; /* 隐藏密码输入框 */
}
button {
display: none; /* 隐藏提交按钮 */
}
```
接下来,在JavaScript中编写功能代码。首先,需要设置一个密码:
```javascript
var password = "your_password";
```
然后,需要编写一个函数来检查输入的密码是否正确,并且显示密码输入框和提交按钮:
```javascript
function checkPassword() {
var inputPassword = document.getElementById("myPassword").value;
if (inputPassword == password) {
document.getElementById("myPassword").style.display = "none";
document.getElementById("myImage").setAttribute("onclick", "window.location.href='your_destination_url'");
} else {
alert("Wrong password!");
}
}
```
以上代码的作用是:获取输入框中的密码,判断是否与预设的密码一致。如果一致,则隐藏密码输入框和提交按钮,并将图片的点击事件设为跳转至目标页面。如果不一致,则弹出提示框。
最后,在图片上添加点击事件,弹出密码输入框:
```javascript
document.getElementById("myImage").addEventListener("click", function() {
document.getElementById("myPassword").style.display = "block";
document.getElementsByTagName("button")[0].style.display = "block";
});
```
以上代码的作用是:为图片添加点击事件,当点击图片时显示密码输入框和提交按钮。
完整的代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>Image Password</title>
<style>
#myPassword {
display: none;
}
button {
display: none;
}
</style>
</head>
<body>
<img src="your_image_path" id="myImage">
<input type="password" id="myPassword">
<button onclick="checkPassword()">Submit</button>
<script>
var password = "your_password";
function checkPassword() {
var inputPassword = document.getElementById("myPassword").value;
if (inputPassword == password) {
document.getElementById("myPassword").style.display = "none";
document.getElementById("myImage").setAttribute("onclick", "window.location.href='your_destination_url'");
} else {
alert("Wrong password!");
}
}
document.getElementById("myImage").addEventListener("click", function() {
document.getElementById("myPassword").style.display = "block";
document.getElementsByTagName("button")[0].style.display = "block";
});
</script>
</body>
</html>
```
将上述代码中的 `your_image_path`、`your_password` 和 `your_destination_url` 替换为实际的图片路径、密码和目标页面链接即可。
阅读全文
相关推荐












<title>更新汽车等级</title> <body> <main> 修改汽车等级 <form id="saveForm"> <input type="hidden" name="id" value="${data.id }"> <label>汽车等级</label> <input type="text" name="cname" value="${data.cname }" class="form-control"> </form> <button type="button" id="save" class="btn btn-primary">确定提交</button> 返回 </main> </main> <script> $("#save").click(function() { $.ajax({ cache : true, type : "post", url : "CategoryServlet?action=updateCategory", data : $("#saveForm").serialize(), async : false, success : function(e) { if (e == 'yes') { alert("修改成功!"); window.parent.location.href = "CategoryServlet?action=CategoryList"; } else { alert("修改失败!"); } } }) }); </script> </body> </html>




