javascript - 字数统计textarejquery脚本初始文本计数onload问题

标签 javascript jquery html count words

我正在使用字数统计文本区域 jquery 脚本,我正在尝试找出如何将字数统计更新为例如2.从预设文本区域“本示例”开始。

(目前显示0)

我可以将焦点设置在它上面并移动光标,但我不知道如何最初更新它,有什么想法吗?

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="js/jQuery.textareaCounter.c.js"></script> 

<textarea id="txtarea" name="text" rows="7" cols="120">this example</textarea>
<script type="text/javascript">
    $("textarea").textareaCounter();
    document.getElementById('txtarea').focus();
    var val = document.getElementById('txtarea').value; //store the value of the element
    document.getElementById('txtarea').value = ''; //clear the value of the element
    document.getElementById('txtarea').value = val; //set that value back. so cursor is at end.
</script>

jquery.min.js 包含:-

(function(a){a.fn.textareaCounter=function(b){var c={limit:10};var b=a.extend(c,b);return this.each(function(){var c,d,e,f;c=a(this);c.after('<span style="font-size: 11px; clear: both; margin-top: 3px; display: block;" id="counter-text">Max. '+b.limit+" words</span>");c.keyup(function(){d=c.val();if(d===""){e=0}else{e=a.trim(d).split(" ").length}if(e>b.limit){a("#counter-text").html('<span style="color: #DD0000;">0 words left</span>');f=a.trim(d).split(" ",b.limit);f=f.join(" ");a(this).val(f)}else{a("#counter-text").html(b.limit-e+" words left")}})})}})(jQuery)

jQuery.textareaCounter.c.js 包含:-

(function(a) {
    a.fn.textareaCounter = function(b) {
        var c = {
            limit: 10
        };
        var b = a.extend(c, b);
        return this.each(function() {
            var c, d, e, f;
            c = a(this);
            c.after('<span style="font-size: 11px; clear: both; margin-top: 3px; display: block;" id="counter-text">' + "0 words</span>");
            c.keyup(function() {
                d = c.val();
                if (d === "") {
                    e = 0
                } else {
                    e = d.replace(/^[\s,.;]+/, "").replace(/[\s,.;]+$/, "").split(/[\s,.;]+/).length;
                }
                if (e > b.limit) {
                    // a("#counter-text").html('<span style="color: #DD0000;">0 words left</span>');
                    // f=a.trim(d).split(" ",b.limit);
                    // f=f.join(" ");
                    // a(this).val(f)
                    a("#counter-text").html(e + " words ")
                    document.myform.numwords.value = e;
                } else {
                    a("#counter-text").html(e + " words ")
                    document.myform.numwords.value = e;
                }
            });
        });
    }
})
(jQuery)

最佳答案

这是我在 jQuery.textareaCounter.c.js 中更改的内容:

var initCount = c.text().split(" ").length;
if(initCount < 2){initCount=0;}
c.after('<span style="font-size: 11px; clear: both; margin-top: 3px; display: block;" id="counter-text">' + initCount +" words</span>");

Here is the JSFiddle demo

关于javascript - 字数统计textarejquery脚本初始文本计数onload问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32073051/

相关文章:

javascript - JQuery 背景颜色操作不起作用

Javascript:在具有淡入淡出效果的循环中更改<img>

jquery - div 相对于其他 DIV 的高度

javascript - HTML 5 视频或音频播放列表

javascript - Puppeteer 在页面中找到数组元素,然后单击

javascript - 链接到每天都会变化的 URL?

javascript - 获取html中选择的位置

javascript - 循环遍历 div 的所有后代 - 仅限 JS

html - 带下拉导航的导航栏未占用整个浏览器宽度

html - 框架 .htaccess 时显示消息