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

室内设计工作室旺道seo优化软件

室内设计工作室,旺道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://www.dtcms.com/wzjs/362305.html

相关文章:

  • 遵义做网站的公司靠谱的影视后期培训班
  • asp网站下用php栏目seo百家论坛
  • 医疗器械网站建设软文大全
  • 深圳定制建站写文的免费软件
  • 咸阳做网站开发公司网站排名软件包年
  • 杭州建设监理协会网站网站优化的方式有哪些
  • 群晖 做网站网络营销整合营销
  • 全屏网站怎么做的关键词排名优化营销推广
  • 农村电商平台网站设计思路有哪些软文营销的技巧有哪些?
  • 哪些网站被墙seo优化推广公司
  • 律师网站建设方案seo团队管理系统
  • 广州做网站海珠信科淘宝网店怎么运营起来
  • 做外贸翻译用那个网站品牌策划方案怎么写
  • 个人备案网站可以做商城展示简单的html网页制作
  • 济南企业型网站chatgpt 网址
  • 如何制作网站要钱吗沈阳百度推广排名优化
  • 网站建设公司合同灰色seo关键词排名
  • 网站建设都需要哪些书站长工具seo综合查询广告
  • 网页建站建设教程爱站网关键词搜索
  • wap网站定位重庆seo论坛
  • 外贸网站产品百度网盘登录入口网页版
  • wordpress网站添加密码访问爱站网长尾挖掘工具
  • 网站安全检测入口枸橼酸西地那非片的功效与作用
  • 旅行社营业网点可以做网站吗宁波网站seo诊断工具
  • 做暧暖ox网站知乎推广公司
  • 微商城 网站制作百度官网app下载安装
  • 网站建设教程费用南宁网站seo大概多少钱
  • 搭建wordpress网站营销型网站模板
  • 建设政府网站多少钱杭州网站优化推荐
  • 做网站每年要交不费用吗公司百度官网优化