【发布时间】:2015-01-30 01:56:21
【问题描述】:
tldr:如何突出显示 javascript 字符串变量中的多个子字符串(可能使用黑色文本可读的不同颜色)?模拟所需输出:
加长版
我有一些文字的跨度:
<span>This is some text, there are lots of things in here. </span>
我还有一个包含要突出显示的子字符串的哈希:
h = {"t0": [0,2], "t1" : [8,15]}
我想突出显示给定子字符串数组的散列开始和结束的文本。它看起来像这样:
这个s是some text,这里有很多东西。
我想过使用html标签,但我不知道如何同时替换所有子字符串:
累积尝试
第一次迭代
<tag1>Thi</tag1>s is some text, there are lots of things in here.
第二次迭代
<tag1>Th<tag2>i</tag1</tag2>>s is some text, there are lots of things in here.
^ this is where the new 8th index is
顺序尝试
如果我使用原始值,我不知道如何将结果相加:
第一次迭代
<tag1>Thi</tag1>s is some text, there are lots of things in here.
第二次迭代
This is <tag2>some tex</tag2>t, there are lots of things in here.
太棒了!但是如何将它们加在一起?
编辑:
标签可以很好地嵌套:
<tag1>hello <tag2>world</tag2></tag1>
不是我的正常用例,但我想它可能会发生。即使它只是用工具提示突出显示,我猜这也会有帮助吗?
【问题讨论】:
-
您可能想查看LetteringJS 来窃取代码。我认为它适用于 DOM 结构。
-
标签是否重叠?
-
不正常,但有时。 (取决于你的意思。)
-
我想“嵌套”是一个更合适的词。标签可以相互嵌套吗?
标签: javascript jquery html ruby-on-rails