【发布时间】:2021-09-24 07:26:16
【问题描述】:
想法:
我希望在选择文本部分而不是更改背景时为文本添加下划线(最好是颜色渐变like here)。根据Mozillatext-decoration是可以的,text-decoration: underline也是可以的。
问题:
(text-decoration 和 ::selection 对我不起作用(Chrome 和 Edge) - 已解决)。
解决思路:
在进行选择(在 CSS 中)时,是否可以将文本下划线作为渐变?我的一个想法是使用JavaScript 来查看是否选择了文本,然后在文本之间添加一个id="underline" 左右,然后包含渐变线的CSS 代码。
进展:
整个事情不是用纯 CSS 实现的。 I have now changed the code, so that when a selection is also the text is suitably underlined.
进度问题:
即使在单击选择后,该行仍然存在。
当前代码:
function wrapSelectedText() {
var selectedText = window.getSelection().getRangeAt(0);
var selection = window.getSelection().getRangeAt(0);
var selectedText = selection.extractContents();
var span = document.createElement("span");
/* Styling */
span.style.backgroundImage = "linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%)";
span.style.backgroundRepeat = "no-repeat";
span.style.backgroundSize = "100% 0.2em";
span.style.backgroundPosition = "0 88%";
span.appendChild(selectedText);
selection.insertNode(span);
}
document.onmouseup = document.onkeyup = document.onselectionchange = function() {
wrapSelectedText();
};
::selection {
background: none;
}
<p>This is an example text.</p>
【问题讨论】:
标签: javascript html css selection underline