完整的字数后,将段落中的跨度增加到150

时间:2019-01-02 07:59:19

标签: javascript jquery

我有一个段落,我想添加一个span标签,这样当字数达到80时,它会在关闭段落标签之前关闭该段落的最后一个span。

1 个答案:

答案 0 :(得分:1)

已更新,以在第80个字之后添加跨度 您可以在p元素内获取文本,并在80个单词之后添加跨度。进行更改后,将旧的p元素替换为新的p元素。

p = document.getElementById("myP");
text = p.innerText;
textArray = text.split(" ");
newtext = textArray.splice(0, 80).join(" ") + " <span> "+ textArray.join(" ") + "</span>";
p.innerHTML = newtext;
span {
  color: red;
}
<p id="myP">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"</p>