【问题标题】:Overlapping of one string into another string highlighting issue一个字符串重叠到另一个字符串突出显示问题
【发布时间】:2018-09-12 07:16:43
【问题描述】:

我有一个字符串,可以像

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

现在,我有一个 json,其中包含我想要突出显示的字符串开始和结束偏移量。现在,我使用的逻辑是这样的--

$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>');
}
//Only if you don't know if they are in the correct order:
jsonDataArray = jsonDataArray.sort((a, b) => a.startOffset - b.startOffset);

for (var i = jsonDataArray.length - 1; i >= 0; i--) {
  const item = jsonDataArray[i];
  responseData = $scope.highlightHTML(responseData, item.startOffset, item.endOffset, item.color);
};
$rootScope.data.htmlDocument = responseData.replace(/\n/g, "</br>");

所以,这里我试图从数组的最后一个值突出显示,这样偏移量就不会改变。现在,这里有一个问题,那就是重叠。现在,让我们说,

在本文中,我通过添加一些 span class 突出显示了Lorem Ipsum has been。现在,对于下一次交互,如果 startoffset 和 endoffset 有一个字符串,它不过是 Ipsum has been the industry's standard 。现在, Here 会有这两个重叠,然后突出显示是重叠的。因此,我无法获得确切的文本,因为偏移量发生了变化。

现在,我应用的另一个解决方案是 -

var length = '<span class="mark"></span>'.length:
jsonDataArray.forEach(function(item, index) {
    responseData = $scope.highlightHTML(responseData, item.startOffset + (index * length), item.endOffset + (index * length), item.color);
});
$rootScope.data.htmlDocument = responseData.replace(/\n/g, "</br>");

但这里也存在同样的问题,例如,如果一件事的某些部分存在于另一件事中,则会产生一些问题。有人可以帮我解决这个问题吗?

【问题讨论】:

标签: javascript jquery css angularjs html


【解决方案1】:

为避免跟踪您的索引移动,我建议您将输出字符串单独存储或存储在一个数组中,如下所示:

const str = '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';

const highlights = [{startOffset: 2, endOffset: 16}, {startOffset: 68, endOffset: 75}, {startOffset: 80, endOffset: 92}];

const result = [];
let currentIndex = 0;

highlights.forEach(h => {
  result.push(str.substring(currentIndex, h.startOffset));
  result.push(`<span class="mark">${str.substring(h.startOffset, h.endOffset)}</span>`);
  currentIndex = h.endOffset;
});

result.push(str.substring(currentIndex, str.length));

document.getElementById('root').innerHTML = result.join('');
.mark {
  color: red;
}
&lt;div id="root"&gt;&lt;/div&gt;

【讨论】:

  • 让我检查一下。
  • 您能否详细说明一下
  • 实际上,我必须向用户显示突出显示的部分以及整个给定的字符串
  • 而 class="mark" 这个类名每次都会不同
  • 这不是我必须显示带有突出显示文本而不是仅突出显示的字符串的完整文档
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2013-05-07
  • 2011-08-31
相关资源
最近更新 更多