作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 angular js
的新手。在这里,我有一个字符串
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。现在,对于下一次交互,如果 startoffset 和 endoffset 的字符串只是 Ipsum 已成为行业标准。现在,这两个重叠,然后突出显示重叠。因此,我无法获得确切的文本,因为偏移量发生了变化。
现在,为此我尝试了以下解决方案 ->
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('')
现在,在这里,它正在解决我的问题,但是如果它重叠,那么 Here A duplicate text is getting added in my string 。如果有一个文本相互重叠,那么它就是将文本分开。但我不想要那种行为。有人可以帮我解决这个问题吗?
最佳答案
我不确定,如果我理解正确你的问题,但你的问题发生了,如果突出显示的区域重叠,像这样?
var highlights = [{startOffset: 2, endOffset: 16},
{startOffset: 85, endOffset: 100},
{startOffset: 80, endOffset: 92}];
要实现这一点,您必须首先生成不同的区域。
highlights.sort(function (a, b) {return a.startOffset - b.startOffset; }); //first order by the start offset
// highlights looks like this
//[{startOffset: 2, endOffset: 16},
// {startOffset: 80, endOffset: 92},
// {startOffset: 85, endOffset: 100}]
// next merge all overlapping areas
for(var i = 0; i < highlights.length - 1; i++) { //each element but the last, because it can't overlap with the next
if(highlights[i].endOffset > highlights[i + 1].startOffset) { //if it overlaps with the next
highlights[i].endOffset = highlights[i].endOffset > highlights[i + 1].endOffset ?
highlights[i].endOffset : highlights[i + 1].endOffset; //take the higher end offset of those two as new offset
highlights.splice(i + 1, 1); //remove next element in list, since it is no longer useful
i--; //check current element again
}
}
//output
//[{startOffset: 2, endOffset: 16},
// {startOffset: 80, endOffset: 100}]
在那之后,您的代码应该可以工作了。
关于javascript - 突出显示文档中的文本重复添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49937396/
有没有一种方法可以“标记”对象的属性,使它们在反射中“突出”? 例如: class A { int aa, b; string s1, s2; public int AA
我是一名优秀的程序员,十分优秀!