600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > js和jq实现点击小眼睛后密码显示与隐藏切换

js和jq实现点击小眼睛后密码显示与隐藏切换

时间:2022-11-25 12:51:18

相关推荐

js和jq实现点击小眼睛后密码显示与隐藏切换

效果(要求)

眼睛的图标这里是使用了阿里巴巴图标库的图标。

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="/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="/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标签中写入口函数,就是为了等页面加载完成后才执行入口函数。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。