gpt4 book ai didi

javascript - 固定字数限制后如何更改文本颜色

转载 作者:行者123 更新时间:2023-11-27 23:40:41 25 4
gpt4 key购买 nike

我必须在固定的字符限制后更改我的 contenteditable 元素文本颜色,我应该怎么做。就像 140 个字符之前的文本将是黑色的,但在 140 个字符之后,将写入的任何文本都会自动变为红色。

function setEndOfContenteditable(contentEditableElement) {
var range, selection;
range = document.createRange(); //Create a range (a range is a like the selection but invisible)
range.selectNodeContents(contentEditableElement); //Select the entire contents of the element with the range
range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start
selection = window.getSelection(); //get the selection object (allows you to change selection)
selection.removeAllRanges(); //remove any selections already made
selection.addRange(range); //make the range you have just created the visible selection
}

elem = document.getElementById('my-content');

elem.addEventListener("keypress", function(evt) {
var _evt = evt;
setTimeout(function() {
_evt = _evt || window.event;
var charCode = _evt.keyCode || _evt.which;
$("#my-content").lettering('words');
setEndOfContenteditable(elem);
}, 10, _evt);
}, false);

最佳答案

检查输入的长度,如果它高于你想要的,改变颜色:

if (elem.value.length > 140) {
elem.style.color = 'red';
} else {
elem.style.color = 'black';
}

我不是很理解这个问题,但希望对您有所帮助。

关于javascript - 固定字数限制后如何更改文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57057090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com