zoukankan      html  css  js  c++  java
  • JavaScript

       实时显示输入英文单词个数

     1  function wordCount(input) {
     2                     // 获取文本框对象
     3                     var el = document.getElementById('words');
     4                     if (el && input) {
     5                         // 获取输入内容长度并更新到界面
     6                         var value = input.value;
     7                         // 替换中文字符为空格
     8                         value = value.replace(/[u4e00-u9fa5]+/g, " ");
     9                         // 将换行符,前后空格不计算为单词数
    10                         value = value.replace(/
    |
    |^s+|s+$/gi, "");
    11                         // 多个空格替换成一个空格
    12                         value = value.replace(/s+/gi, " ");
    13                         // 更新计数
    14                         var length = 0;
    15                         var match = value.match(/s/g);
    16                         if (match) {
    17                             length = match.length + 1;
    18                         } else if (value) {
    19                             length = 1;
    20                         }
    21                         el.innerText = length;
    22                     }
    23                 }

    html

    1 <body>
    2 <div onclick="gaibian()">单词数:<span id="words">0</span></div>
    3 <textarea id="content" onkeyup="wordCount(this);"></textarea>
    4 </body>

    js简易选项卡

     1 function xianshinews() {
     2             document.getElementById("xinwen").style.display = "block"
     3             document.getElementById("zhaiyao").style.display = "none"
     4             document.getElementById("top").style.display = "none"
     5 
     6         }
     7 
     8         function xianshiabstract() {
     9             document.getElementById("zhaiyao").style.display = "block"
    10             document.getElementById("xinwen").style.display = "none"
    11             document.getElementById("top").style.display = "none"
    12 
    13         }
    14 
    15         function xianshitop() {
    16             document.getElementById("top").style.display = "block"
    17             document.getElementById("zhaiyao").style.display = "none"
    18             document.getElementById("xinwen").style.display = "none"
    19 
    20         }

    html

    <div id="box">
        <div id="hd">
            <span id="s1" onmouseover="xianshinews()">news</span>
            <span id="s2" onmouseover="xianshiabstract()">abstracts</span>
            <span id="s3" onmouseover="xianshitop()">tops</span>
        </div>
        <div id="bd">
            <div class="info" id="xinwen">
                <ul>
                    <li><a href="#">newsnewsnewsnewsnewsnews</a></li>
                </ul>
            </div>
            <div class="info" id="zhaiyao">
                <ul>
                    <li><a href="#">abstractsabstractsabstractsabstractsabstracts</a></li>
                </ul>
            </div>
            <div class="info" id="top">
                <ul>
                    <li><a href="#">topstopstopstopstopstopstops</a></li>
                </ul>
            </div>
        </div>
    </div>
  • 相关阅读:
    (转)Silverlight 与 JS交互
    使用wcf服务捕捉到“POST http://yourIP/WCFService.svc 405 (Method Not Allowed) ”错误!
    Silverlight 页面传值问题(转)
    (转)发布Silverlight+WCF程序到IIS后,客户端访问数据库失败的解决方案
    Static Function Test
    .net 开发者尝试Apache Spark™
    Ubuntu 16.04.2 LTS 安装 jdk1.6 和 tomcat6 (一)
    Ubuntu 16.04.2 LTS 安装 jdk1.6 和 tomcat6 (二)
    Win10+VS2015折腾小记
    经验不是万能的----驴子驮盐
  • 原文地址:https://www.cnblogs.com/wlc297984368/p/7634296.html
Copyright © 2011-2022 走看看