简体   繁体   English

如何突出显示HTML文本而不用标签包装它?

[英]How to highlight HTML text without wrapping it with tags?

Is it possible to highlight text in an HTML document using without wrapping it with <span> or any other tag for that matter? 是否可以使用<span>或任何其他标记来突出显示HTML文档中的文本?

For example, in the HTML code <p>The quick fox</p> I would like to highlight quick but without adding a DOM element around it. 例如,在HTML代码<p>The quick fox</p>我想quick突出显示但不添加围绕它的DOM元素。 Adding a DOM element to a parent element is fine. 将DOM元素添加到父元素就可以了。

Thanks! 谢谢!

No, it is not possible. 不,这是不可能的。

You can't tell the browser to render a piece of text differently without inherently changing the DOM, regardless of whether you do it statically or dynamically (with Javascript, for example, as a post processing step). 无论是静态还是动态地改变DOM,您都无法告诉浏览器以不同方式呈现一段文本(例如,使用Javascript作为后处理步骤)。

It is possible if you use an absolutely positioned element with a transparent repeating background image or a transparent background color (using rgba or hsla) and position it over the selected area. 如果您使用具有透明重复背景图像或透明背景颜色(使用rgba或hsla)的绝对定位元素并将其放置在所选区域上,则可以使用此选项。

Another way to do it would be to have an absolutely positioned canvas element without a background that takes up the whole browser viewport and draw a transparent rectangle over the selection. 另一种方法是使一个绝对定位的画布元素没有背景,占据整个浏览器视口并在选区上绘制一个透明矩形。

It's not possible. 这是不可能的。

If you just want no tags in the original source code, it might be possible by adding tags later using Javascript magic. 如果您只想在原始源代码中不使用标记,则可以稍后使用Javascript魔术添加标记。 You could do something like 你可以做点什么

<p highlight="quick">The quick fox</p>

and write a JQuery/Prototype/plain JS function to highlight it on the fly, but what for and why? 并编写一个JQuery / Prototype / plain JS函数来动态突出显示它,但是为什么以及为什么? If you elaborate a bit, someone may come up with an idea. 如果你详细说明一下,有人可能想出一个主意。

唯一能做到这一点的方法就是使用<canvas>元素,并手工渲染绝对的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM