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

时间:2019-07-16 12:17:14

标签: javascript jquery html css

在固定字符数限制后,我必须更改我的内容可编辑元素的文本颜色。例如,在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);

1 个答案:

答案 0 :(得分:0)

检查输入的长度,如果颜色高于所需的颜色,则更改颜色:

-pthread

我不太了解这个问题,但希望能有所帮助。