[英]Change the color of text selection
I understand that there is a CSS pseudo-element ::seleсtion
. 我知道有一个CSS伪元素
::seleсtion
。 But nevertheless, I need it, because I learn JavaScript. 但是尽管如此,我还是需要它,因为我学习JavaScript。 Here's the code to which I came, but it does not work:
这是我所使用的代码,但是它不起作用:
document.body.addEventListener('mouseup', function() {
var txt = window.getSelection().toString();
var txtNode = document.createTextNode(txt);
var rng = document.createRange();
rng.setStart(txtNode, 0);
rng.setEnd(txtNode, txt.length);
var span = document.createElement('span');
span.style.backgroundColor = 'green';
rng.surroundContents(span);
}, false);
Until that happened only highlight. 在此之前,只有突出显示。 What more do to when clicking on an empty space allocation reset?
单击空白空间分配重置时该怎么办?
document.body.addEventListener('mouseup' , function(){
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var span = document.createElement('span');
span.style.backgroundColor = 'green';
range.surroundContents(span);
} , false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.