我试图实现一个功能,当用户选择一个文本,用户可以改变文本的颜色使用颜色选择器,这种改变应该是永久的,直到他/她再次选择文本改变颜色。我能够改变整个文本的颜色,但不能弄清楚如何改变选择文本。我在StackOverflow上检查了很多问题,但我无法找到解决方案。这是我的档案
var box = document.getElementById('Myelement');
let colorpicker = document.getElementById('ColorPicker1');
setInterval(() => {
let color = colorpicker.value;
box.style.color = color;
}, 200);
/* function selectText(hexColor) {
var selection = window.getSelection().getRangeAt(0);
var selectedText = selection.extractContents();
var span = document.createElement('span');
span.style.color = hexColor;
span.className = 'selected-text';
span.appendChild(selectedText);
selection.insertNode(span);
}
function unselectText(){
$('#Myelement').find('.selected-text').contents().unwrap();
}
$(document).on('mouseup', function () {
selectText('#f90');
});
$(document).on('mousedown', function () {
unselectText();
});
*/
<html>
<head>
</head>
<body>
<p id="Myelement" contenteditable = "true">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<input name="MyColorPicker"
type="color"
id="ColorPicker1" />
<script>
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</body>
</html>
2条答案
按热度按时间wswtfjt71#
你基本上是在那里,除了这是一个
input
事件的颜色选择器fnvucqvd2#