【发布时间】:2019-02-10 20:02:25
【问题描述】:
我需要将光标放在输入的最后,因为它在开始时会突出显示文本以便开始。
我尝试使用this.selectionStart = this.selectionEnd = this.value.length;,但效果不佳,或者我将其放在错误的行中。
谢谢:)
window.setInterval(function() {
change();
}, 1);
function change() {
$(function() {
var elem = $('#code');
highlighter(' new ', elem, "red");
highlighter(' button', elem, "blue");
});
function highlighter(word, elem, color) {
var html = elem.html();
var reg = new RegExp(word, "g");
html = html.replace(reg, "<span class='" + color + "'>" + word + "</span>");
elem.html(html);
}
}
.red {
color: red;
}
.blue {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="code" contenteditable="true">
new button
</div>
【问题讨论】:
标签: javascript jquery html highlight