【发布时间】:2019-02-15 19:51:28
【问题描述】:
我的网站上有一个富文本编辑器,我正在尝试为它创建一个可靠的字数计数器。
因为它是一个富文本编辑器,它(可能)包含 html。
这个 html 可能是,例如:
<div class="textEditor">
<h1><strong>this is a sample heading</strong></h1>
<p><br></p>
<p>and this is a sample paragraph</p>
</div>
为了获得可靠的字数,我首先尝试使用以下方法将 html 转换为文本:
var value = $('.textEditor').text()
我面临的问题是返回的字符串似乎在删除 html 标记的位置连接起来,而我剩下的是:
this is a sample headingand this is a sample paragraph
如您所见,“标题”“和”这两个词被连接成“标题和”,这将使我的字数变为 10 而不是 11。
任何关于如何正确实现这一点的想法将不胜感激:)
【问题讨论】:
-
看起来程序忽略了空格以外的空格。可能会提供线索
标签: javascript jquery html string concatenation