【问题标题】:How to calculate the number of characters(spaces are excluded) within a pair of tags in JQuery or Javascript?如何计算 JQuery 或 Javascript 中一对标签内的字符数(不包括空格)?
【发布时间】:2009-11-19 12:44:03
【问题描述】:

如何计算JQuery或Javascript中一对标签内的字符(不包括空格)?什么功能?

例如:

<pre id="mytext">This is the text.      This is the text.</pre>

如何知道$("#mytext")中有多少个字符或单词?

【问题讨论】:

    标签: javascript jquery spaces


    【解决方案1】:

    像这样:

    var chars = $('#mytext').text().replace(/ /g, '').length;
    

    如果您想排除任何空格,而不仅仅是空格:

    var chars = $('#mytext').text().replace(/\s/g, '').length;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-21
      • 2015-02-06
      • 2023-03-17
      • 1970-01-01
      • 2021-12-27
      • 2018-03-04
      • 1970-01-01
      • 2019-05-27
      相关资源
      最近更新 更多