当前位置: 首页 > wzjs >正文

哪做网站好建设电子商务平台网站

哪做网站好,建设电子商务平台网站,西安微信公众号制作,黑帽seo寄生虫在现代 Web 应用中,允许用户切换密码的可见性不仅提升了用户体验,也让表单填写更便捷。使用 TypeScript 来实现这个功能,不仅具备强类型检查优势,还能提升代码的可维护性。 ✨ 我们要实现的功能 在这篇文章中,我们将…

在现代 Web 应用中,允许用户切换密码的可见性不仅提升了用户体验,也让表单填写更便捷。使用 TypeScript 来实现这个功能,不仅具备强类型检查优势,还能提升代码的可维护性。

✨ 我们要实现的功能

在这篇文章中,我们将使用 TypeScript 构建一个简单的密码输入组件,具备以下特性:

  • 用户可以在输入框中输入密码;
  • 通过按钮切换密码显示或隐藏;
  • 按钮图标会根据状态更新;
  • 在隐藏状态下密码仍保持安全。

🔧 HTML + CSS 页面结构与样式

以下是页面的 HTML 与 CSS 代码,包含密码输入框与切换按钮的基本布局和样式:

<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>显示与隐藏密码</title><style>body {font-family: Arial, sans-serif;background-color: #f9f9f9;display: flex;justify-content: center;align-items: center;height: 100vh;margin: 0;}.container {background: #fff;padding: 20px;border-radius: 8px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);width: 300px;text-align: center;}h1 {font-size: 24px;color: #333;}.input-group {display: flex;flex-direction: column;align-items: center;width: 100%;}.input-container {position: relative;width: 100%;}input {width: 80%;padding: 10px;font-size: 16px;border: 1px solid #ccc;border-radius: 4px;padding-right: 40px;}button {background: transparent;border: none;cursor: pointer;position: absolute;right: 35px;top: 50%;transform: translateY(-50%);font-size: 18px;}button:focus {outline: none;}</style></head><body><div class="container"><h1>请输入密码</h1><form id="passwordForm"><div class="input-group"><div class="input-container"><input type="password" id="password" placeholder="请输入您的密码" /><button type="button" id="toggleVisibility" aria-label="显示密码">👁️</button></div></div></form></div></body>
</html>

📌 说明:

  • 输入框 input[type="password"] 用于接收用户密码;
  • 按钮位置使用 position: absolute 放在输入框右侧;
  • 默认使用图标 👁️ 表示“显示密码”,点击后变为 🙈 表示“隐藏密码”。

🧠 TypeScript 实现逻辑

以下是核心的 TypeScript 代码,用于实现点击按钮时切换密码可见性:

const inp = document.getElementById('password') as HTMLInputElement | null;
const btn = document.getElementById('toggleVisibility') as HTMLButtonElement | null;if (inp && btn) {btn.addEventListener('click', () => {if (inp.type === 'password') {inp.type = 'text';btn.textContent = '🙈'; // 显示密码} else {inp.type = 'password';btn.textContent = '👁️'; // 隐藏密码}});
}

📝 核心逻辑解释:

  • 使用 getElementById 获取密码输入框和按钮元素;
  • 监听按钮点击事件;
  • 根据当前输入框的 type 类型切换为 "text""password"
  • 同时切换按钮的图标。

🔄 编译 TypeScript 为 JavaScript

浏览器无法直接运行 TypeScript,需要将 .ts 文件编译为 .js 文件,使用以下命令:

tsc task.ts
# 或者使用 npx
npx tsc task.ts

编译后将生成 task.js 文件,可通过 <script src="task.js"></script> 引入页面中。

✅ 最终完整代码(使用 JavaScript)

以下是最终的 HTML 文件,已内嵌 JavaScript 实现密码显示/隐藏功能:

<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>显示与隐藏密码</title><style>body {font-family: Arial, sans-serif;background-color: #f9f9f9;display: flex;justify-content: center;align-items: center;height: 100vh;margin: 0;}.container {background: #fff;padding: 20px;border-radius: 8px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);width: 300px;text-align: center;}h1 {font-size: 24px;color: #333;}.input-group {display: flex;flex-direction: column;align-items: center;width: 100%;}.input-container {position: relative;width: 100%;}input {width: 80%;padding: 10px;font-size: 16px;border: 1px solid #ccc;border-radius: 4px;padding-right: 40px;}button {background: transparent;border: none;cursor: pointer;position: absolute;right: 35px;top: 50%;transform: translateY(-50%);font-size: 18px;}button:focus {outline: none;}</style></head><body><div class="container"><h1>请输入密码</h1><form id="passwordForm"><div class="input-group"><div class="input-container"><input type="password" id="password" placeholder="请输入您的密码" /><button type="button" id="toggleVisibility" aria-label="显示密码">👁️</button></div></div></form></div><script>var inp = document.getElementById('password');var btn = document.getElementById('toggleVisibility');if (inp && btn) {btn.addEventListener('click', function () {if (inp.type === 'password') {inp.type = 'text';btn.textContent = '🙈';} else {inp.type = 'password';btn.textContent = '👁️';}});}</script></body>
</html>

🏁 总结

通过本教程,你已经学会了如何使用 TypeScript 和 HTML/CSS 构建一个简洁的密码可见性切换功能,适用于任何需要用户登录、注册或修改密码的表单页面。你也可以进一步扩展功能,比如:

  • 添加密码强度检测;
  • 集成密码生成器;
  • 在移动端做响应式优化。
     

——未完待续——


文章转载自:

http://39ZNEmVj.cfjyr.cn
http://npDXJpp2.cfjyr.cn
http://KAoHrFHJ.cfjyr.cn
http://l0jhFtEN.cfjyr.cn
http://ORi9iiih.cfjyr.cn
http://WHPcXNSb.cfjyr.cn
http://1CDZDKlD.cfjyr.cn
http://WuMNPyFA.cfjyr.cn
http://F0JIUu2M.cfjyr.cn
http://Y1xpWlQC.cfjyr.cn
http://4AEf34lm.cfjyr.cn
http://DKs2lcme.cfjyr.cn
http://3QcOCi9e.cfjyr.cn
http://Qj16xaSb.cfjyr.cn
http://4sJYQiye.cfjyr.cn
http://s6BtFIMd.cfjyr.cn
http://21bKEA7s.cfjyr.cn
http://7iFxSi0w.cfjyr.cn
http://drixybTr.cfjyr.cn
http://OteFG6mT.cfjyr.cn
http://gwFGU8Sg.cfjyr.cn
http://AVC70v2h.cfjyr.cn
http://RiLkhiuP.cfjyr.cn
http://PxXNoDju.cfjyr.cn
http://OhrB7zTb.cfjyr.cn
http://g238i4mO.cfjyr.cn
http://Ogek5r6a.cfjyr.cn
http://JliVQvvT.cfjyr.cn
http://vPnZnzfz.cfjyr.cn
http://m405GqBc.cfjyr.cn
http://www.dtcms.com/wzjs/647804.html

相关文章:

  • 优化企业门户网站百度搜索优化怎么做
  • 域名解析 网站wordpress popular posts怎么用
  • 中国建设银行网上银行网站特点淄博网站运营公司
  • 影视网站如何做seo天元建设集团有限公司张琥超
  • 手机网站左右滑动效果深圳专业商城网站设计制作
  • 重庆网站建设熊掌号公司网站建站软件
  • 怎么在wordpress建站网站 转成 微信小程序
  • 广东营销网站建设服务公司企业培训课程有哪些
  • 中国互联网站建设中心wordpress哪些文件需要给777
  • 深圳外贸建网站wordpress 形式修改
  • 1150网站建设服务器主板做鞋用什么网站好
  • 网站备案 网址销售型网站设计
  • 网站建设怎样做好erp沙盘模拟
  • 专业的外贸网站建设公司排名贵州城乡建设厅城乡建设网站
  • 淘宝客如何做自己的网站八爪鱼wordpress
  • 爱站网 关键词挖掘工具站单页网站怎么制作教程
  • 介休网架公司网站常用的优化方法有哪些
  • 小游戏网站开发者门户网站建设需求
  • xuzhou公司网站制作太原手手工网站建设公司
  • 网站需求分析模板专业的大良网站设计
  • dw可以做网站吗视频制作素材网站
  • 宝安网站建设(深圳信科)浏览器无法上网但有网
  • 网站建设材料中国建筑人事部大全
  • 如皋做公司网站厦门网站关键词推广
  • 番禺做网站技术wordpress 部分图打不开
  • 广州企立科技做网站百度seo如何优化
  • 网站开发需要资质吗湖南省建设厅电话号码是多少
  • 免费学校网站模板html近期国外重大新闻事件
  • 建设路第3小学网站电话网站设计团队介绍
  • 网站开发常见模块互动营销的案例有哪些