问题
Is it possible to highlight text in an HTML document using without wrapping it with <span>
or any other tag for that matter?
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. Adding a DOM element to a parent element is fine.
Thanks!
回答1:
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).
回答2:
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.
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.
回答3:
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. 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? If you elaborate a bit, someone may come up with an idea.
回答4:
The only way to do this than I can imagine would be to use the <canvas>
element, and render absolutely everything by hand.
来源:https://stackoverflow.com/questions/1816308/how-to-highlight-html-text-without-wrapping-it-with-tags