【问题标题】:While highlighting the text from a string using spans creating problems使用 span 突出显示字符串中的文本时会产生问题
【发布时间】:2018-03-23 04:38:15
【问题描述】:

我是webDevelopment 的新手。我有一个字符串,我想突出显示字符串的某些部分,例如我想突出显示的 10-15 件事。现在我也有了偏移量,比如我想从该字符串中突出显示的文本的开头和结尾。因此,当在循环中首先突出显示时,它会在其中添加带有类标记的 span 标签,因此索引正在更改,然后当它尝试突出显示第二个时,它没有得到完美匹配,因为现在偏移量是改变了。那么,如何将确切的文本与 span 标签匹配或不匹配?

$scope.highlight = function(content,startoffset,endoffset){return content.replace(content.substring(startoffset, endoffset), '<span class="' + className + '">$&</span>');}

.mark {background-colour = yellow;}

谁能帮帮我,这对我来说真的很麻烦。

【问题讨论】:

  • 不要替换原始字符串,将更改应用到临时字符串。
  • 你能告诉我该怎么做吗

标签: javascript jquery html css angularjs


【解决方案1】:

请参阅此https://plnkr.co/edit/j5VCCjCHN60l0QNSTtLo?p=preview。我用“**”替换了您的标记,因为它很容易显示示例示例。

  function stringReplace(content, startoffset, endoffset, previousOffset) {
    _temp = _temp.concat(content.substring(previousOffset || 0, startoffset));
    _temp = _temp.concat('**');
  }

【讨论】:

  • 每件事都在一个 p 标签中,它是一个字符串
  • 使用_temp,它有完整的字符串。
猜你喜欢
  • 1970-01-01
  • 2019-09-13
  • 2011-07-02
  • 2015-06-30
  • 2013-07-24
  • 1970-01-01
  • 2018-02-10
  • 2016-11-28
  • 1970-01-01
相关资源
最近更新 更多