字符计数不适用于动态创建的元素

character counting not works for the dynamically created elements

本文关键字:创建 元素 动态 适用于 不适用 字符      更新时间:2023-09-26

我正在使用charcount.js来计算在文本区域中输入的字符数。。

    <textarea class="counter" rows="3"></textarea>

在jquery中,我称之为

    $('.counter').charCount();

它对我有效…当我在javascript中动态创建文本区域时,它不起的作用

    var txtar=document.createElement("textarea");
    txtar.setAttribute("maxlength","140");
    txtar.setAttribute("class","counter");
    ad.appendChild(txtar);

文本区域正确添加到文档中,但charcount函数不起作用。。。。。

我如何解决这个

提前感谢

元素被附加到DOM后,是否调用charCount()?你应该是.