效果(要求)

眼睛的图标这里是使用了阿里巴巴图标库的图标。
js实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>密码框后面的小眼睛</title>
<link rel="stylesheet" href="http://at.alicdn.com/t/font_3448045_gwooawlcz3s.css">
</head>
<body>
<div>
<input type="text" id="pwd">
<i class="iconfont icon-eye-fill" id="eye" onclick="change()"></i>
</div>
<script>
var pwd = document.getElementById("pwd");
var eye = document.getElementById("eye");
function change(){
if( pwd.type == "text"){
pwd.type = "password";
eye.class = "iconfont icon-no_eye"
}
else{
pwd.type = "text";
eye.class = "iconfont icon-eye-fill"
}
}
</script>
</body>
</html>
jq实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>密码框后面的小眼睛</title>
<link rel="stylesheet" href="http://at.alicdn.com/t/font_3448045_gwooawlcz3s.css">
<script src="../js/jquery.min.js"></script>
</head>
<body>
<div>
<input type="text" id="pwd">
<i class="iconfont icon-eye-fill" id="eye"></i>
</div>
<script>
var pwd = $("#pwd");
var eye = $("#eye");
eye.on('click', function() {
if (pwd.attr("type") == "text") {
pwd.attr("type", "password");
eye.attr("class", "iconfont icon-no_eye");
} else {
pwd.attr("type", "text");
eye.attr("class", "iconfont icon-eye-fill");
}
})
</script>
</body>
</html>
一个我的疑问——jq入口函数必须要写吗?
在< head> </ head>标签中,jQuery入口函数必须要写,在< body> </ body>可以不写。
写上入口函数后不论放在哪个标签下都能去执行。一般建议在body标签中写入口函数,就是为了等页面加载完成后才执行入口函数。
这篇博客展示了如何使用纯JavaScript和jQuery实现密码输入框后面的‘小眼睛’图标来切换显示和隐藏密码的功能。通过修改input元素的type属性和图标类名,实现了点击图标切换密码可见性的效果。同时探讨了jQuery入口函数的位置选择及其作用,指出其通常应在页面加载完成后执行以确保DOM就绪。

2201

被折叠的 条评论
为什么被折叠?



