惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
Forbes - Security
Forbes - Security
雷峰网
雷峰网
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
V
Visual Studio Blog
月光博客
月光博客
博客园 - Franky
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
S
SegmentFault 最新的问题
博客园 - 司徒正美
P
Proofpoint News Feed
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
Cyberwarzone
Cyberwarzone
Simon Willison's Weblog
Simon Willison's Weblog
U
Unit 42
P
Proofpoint News Feed
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
P
Palo Alto Networks Blog
V
V2EX
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tailwind CSS Blog
V
Vulnerabilities – Threatpost
Latest news
Latest news
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
博客园 - 【当耐特】
B
Blog RSS Feed
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
NISL@THU
NISL@THU
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security

博客园 - 小院里的霍大侠

从一键部署热门游戏幻兽帕鲁到探索未来个人元宇宙 教你用JavaScript实现调皮的字母 教你用JavaScript实现乘法游戏 教你用JavaScript实现搜索展开 教你用JavaScript实现进度条 教你用JavaScript实现计数器 教你用JavaScript实现背景图像滑动 教你用JavaScript实现鼠标特效 教你用JavaScript获取大转盘 教你用Java实现动态调色板 教你用JavaScript实现表情评级 教你用CSS实现表单部件 Web入门:JavaScript文字动画 教你用JavaScript实现粘性导航 教你用JavaScript随机生成密码 教你用JavaScript完成轮播图 教你用Python制作BMI计算器 教你用JavaScript实现随机点名 三官庙巷-你记忆中的那条路?!
教你用JavaScript实现实时字符计数器
小院里的霍大侠 · 2022-12-13 · via 博客园 - 小院里的霍大侠

案例介绍

欢迎来到我的小院,我是霍大侠,恭喜你今天又要进步一点点了!
我们来用JavaScript编程实战案例,做一个实时字符计数器。用户在指定位置打字,程序实时显示字符数量。

案例演示

在编辑框内输入字符,下方实时记录数字,且输入有数量限制,输入超出限制的字符后就无法再继续输入。

源码学习

进入核心代码学习,我们先来看HTML中的核心代码。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>小院里的霍大侠</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <!-- 有个小院-兴趣编程 -->
    <div class="container">
      <h2>有个小院-实时字符计数器</h2>
      <textarea
        id="textarea"
        class="textarea"
        placeholder="请在这里输入"
        maxlength="50"
        ></textarea>
      <div class="counter-container">
        <p>
          字符数:
          <span class="total-counter" id="total-counter"></span>
        </p>
        <p>
          字符总数:
          <span class="remaining-counter" id="remaining-counter"></span>
        </p>
      </div>
    </div>
    <script src="index.js"></script>
  </body>
</html>

然后再让我们来看CSS代码,由于CSS代码不是重点且数量较多在这里就不做过多介绍。

body {
  margin: 0;
  display: flex;
  justify-content: center;
  height: 100vh;
  align-items: center;
  background-color: salmon;
  font-family: cursive;
}

.container {
  background-color: lightpink;
  width: 400px;
  padding: 20px;
  margin: 5px;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  }

  .textarea {
  resize: none;
  width: 100%;
  height: 100px;
  font-size: 18px;
  font-family: sans-serif;
  padding: 10px;
  box-sizing: border-box;
  border: solid 2px darkgray;
  }

  .counter-container {
  display: flex;
  justify-content: space-between;
  padding: 0 5px;
  }

  .counter-container p {
  font-size: 18px;
  color: gray;
  }

  .total-counter {
  color: slateblue;
  }

  .remaining-counter {
  color: orangered;
  }

让我们来编写核心的JavaScript代码,通过getElementById绑定HTML元素;编写键盘事件,当用户敲击键盘输入字符,则更新字符数量;编写更新字符数量函数,设置字符数为文本框的输入字符长度,设置字符总数为文本框最大长度-字符数。

//有个小院-兴趣编程
const textareaEl = document.getElementById("textarea");
const totalCounterEl = document.getElementById("total-counter");
const remainingCounterEl = document.getElementById("remaining-counter");

textareaEl.addEventListener("keyup", () => {
  updateCounter();
});

updateCounter()

function updateCounter() {
  totalCounterEl.innerText = textareaEl.value.length;
  remainingCounterEl.innerText =
    textareaEl.getAttribute("maxLength") - textareaEl.value.length;
}


记得关注我,每天学习一点点

你觉得这个案例还能应用到什么地方?

全网可搜:小院里的霍大侠, 免费获取简单易懂的实战编程案例。编程/就业/副业/创业/资源。
私微信:huodaxia_xfeater
二维码: http://www.yougexiaoyuan.com/images/weixin_huodaxia.jpg
公众号:有个小院(微信公众号:yougexiaoyuan)
github:yougexiaoyuan (视频源码免费获取)
(部分素材来源于互联网,如有保护请联系作者)