这些字符串可能是很长的段落,所以我不确定最好用空格分隔符分割整个字符串。我正在尝试获取前 10 个单词并将它们包装在一个跨度中:
'<span class="easing">' + string + '</span>'
然后重新加入原始拆分的后半部分。关于超有效方法的建议?它一次最多会影响页面上的三个段落。
已编辑
这是一个踢球者 - 拆分应该发生在第 9 个单词之后或第一个句子的末尾(如果该句子少于 9 个单词)。
例子
var origString = 'Coming into the world on Elvis’ birthday with a doctor named Presley seemed fortuitous until, wielding the silvery smooth scalpel in his aged unsteady hand, the doctor sliced through the walls of my mother’s uterus and into my unborn skin. Inside the warm soothing waters of my mother’s womb, inside the silent weightlessness, I was safe. Then the prick of cold steel marked the first in a series of rude awakenings. I was scarred for life even before birth.';
var newString = '<span="easing">Coming into the world on Elvis’ birthday with a doctor</span> named Presley seemed fortuitous until, wielding the silvery smooth scalpel in his aged unsteady hand, the doctor sliced through the walls of my mother’s uterus and into my unborn skin. Inside the warm soothing waters of my mother’s womb, inside the silent weightlessness, I was safe. Then the prick of cold steel marked the first in a series of rude awakenings. I was scarred for life even before birth.';
或者用一个简短的句子开始段落:
var origString = '“Is he okay? Tell me everything’s okay” she pleas, her desperate need to confirm my health competing with her own need for consolation.';
var newString = '<span class="easing">“Is he okay?</span> Tell me everything’s okay” she pleas, her desperate need to confirm my health competing with her own need for consolation.';