How to highlight HTML text without wrapping it wit

2019-06-21 08:30发布

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!

4条回答
贪生不怕死
2楼-- · 2019-06-21 08:59

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).

查看更多
够拽才男人
3楼-- · 2019-06-21 09:01

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楼-- · 2019-06-21 09:01

The only way to do this than I can imagine would be to use the <canvas> element, and render absolutely everything by hand.

查看更多
▲ chillily
5楼-- · 2019-06-21 09:06

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.

查看更多
登录 后发表回答