【发布时间】:2018-10-02 10:11:10
【问题描述】:
我是angular js和javascript的新手。在这里,我有文档或文本文件,可以说是 ->
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
现在,在这个文件中,我想突出显示部分
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and
所以,为了从后端突出显示此文本,我得到了偏移量,我的意思是该字符串的 startoffset 和 endoffset。
那么,那我用下面的方法来高亮文字。
$scope.highlightHTML(responseData, startOffset, endOffset);
$scope.highlightHTML = function(content, startoffset, endoffset) {
var className = 'mark';
console.log(content.substring(startoffset, endoffset));
return content.replace(content.substring(startoffset, endoffset), '<span class="' + className + '">$&</span>');
}
所以,它用 span 标签替换了这个文本。现在,正因为如此,在我的数组中,我有超过 1 个需要在本文中突出显示的元素。 因此,我从后端获得的偏移量会发生变化。因此,我使用了以下逻辑 ->
var length = '<span class="mark"></span>'.length:
jsonDataArray.forEach(function(item, index) {
responseData = $scope.highlightHTML(responseData, item.startOffset + (index * length), item.endOffset + (index * length));
});
但这是主要问题。
现在让我们说现在我想突出显示文本1500s,现在在这里当涉及到找到这个字符串的偏移量时,它将与新的乘法逻辑不匹配。因为这个1500's 部分是以前突出显示的字符串。
前一个字符串将是
<span class="mark">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and</span>
现在我想突出显示1500s。然后,如果我相乘,那么它将不会采用完美的偏移量,因此不会使字符串突出显示。
那么,我该如何解决这个问题?谁能帮我解决这个问题?
【问题讨论】:
-
您认为plnkr.co/edit/h2mh1MuJGzxOaYFLU7Up?p=preview 可能有帮助吗?如果是,我可以提供建议。您可以使用相同的功能添加颜色或突出显示
-
是的,任何想法都可以拯救我的一天
标签: javascript jquery html css angularjs