在textarea中计数字符仍然不起作用 [英] counting chars in a textarea still not working

查看:47
本文介绍了在textarea中计数字符仍然不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我仍在尝试为文本区域实现快速字符计数器.问题是即使我更改了.innerHTML而不是我进行的.textContent
更改后,它也不起作用 并插入了document.all [''control''],我使用了getElementByID.
我在此处附加代码,请帮助我在IE以外的其他浏览器中使用此代码.

hey guys.
i''m still trying to implement a quick char counter for a text area. problem is its not working even after changes i made instead of .innerHTML i worte .textContent
and insted of document.all[''control''] i used getElementByID.
i''m attaching the code here, please help me get this working in other browsers except IE .

function counter(method,cnt)    //accepts lang method and calc num of msgs
    {
        switch(method)
        {
            case 1 :
                if(cnt > 0 && cnt <= 70)
                    document.all('msgCounter').textContent = "1";
                if(cnt > 70 && cnt <= 134)
                    document.all('msgCounter').innerHTML = "2";
                if(cnt > 134 && cnt <= 201)
                    document.all('msgCounter').innerHTML = "3";
                if(cnt > 201 && cnt <= 268)
                    document.all('msgCounter').innerHTML = "4"; //tmp - need to determine limitation for msgs
                break;
            case 2 :
                if(cnt > 0 && cnt <= 160)
                    document.all('msgCounter').innerHTML = "1";
                if(cnt > 160 && cnt <= 306)
                    document.all('msgCounter').innerHTML = "2";
                if(cnt > 306 && cnt <= 459)
                    document.all('msgCounter').innerHTML = "3";
                break;
        }
        document.getElementById('chrCounter').innerHTML = cnt;
    }

推荐答案

moses12345写道:
moses12345 wrote:

…插入了文档. all [''control'']我使用了getElementByID

…insted of document.all[''control''] I used getElementByID

否,您没有.您看到自己的代码了吗?您仍然使用document.all,这不是合法或标准属性.此外,您可以在多个位置进行计算,但是只能计算一次.您可以在函数counter的第一行中计算DOM元素(在本例中为textarea控件),或者将其作为该函数的参数传递,然后在调用方中使用来计算一次控件引用document.getElementById(不是不是 document.getElementByID,请仔细查看).

您还需要回答我在评论中提出的问题,首先,请您自己回答.您需要添加一个循环,迭代抛出控件的value,增加一些字符计数器.您还需要返回计数器值.您不要尝试执行以上任何操作.

—SA

No, you did not. Did you see you own code? You still use document.all, which is not a legal or standard property. Also, you calculate in several places, but you should calculate it only once. You can calculate the DOM element (textarea control, in this case), in the very first line of the function counter or pass it as a parameter of this function, and calculate the control reference once, in the caller, using document.getElementById (which is not document.getElementByID, look thoroughly).

You also need to answer the questions I asked in my comment to the question, first of all, answer for yourself. You need to add a loop, iterate throw the control''s value, increment some character counter. You also need to return the counter value. You don''t try to do anything of the above.

—SA


这篇关于在textarea中计数字符仍然不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆