有像这样的HTML:
<div contenteditable="true" class="value research-form thumbnail">
Some text here
</div>
Run Code Online (Sandbox Code Playgroud)
div的内容应该动态地突出显示某些单词,而用户类型例如:
<div contenteditable="true" class="value research-form thumbnail">
Some text here <span style="background-color: yellow">highlight</div> it
</div>
<script>
$(document).ready(function () {
var input = $('#textarea').on('input', function (event) {
var newText = input.text().replace('highlight', '<span style="background-color: yellow">highlight</div>');
input.html($.parseHTML(newText));
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
但是有一个问题:当我在div光标中刷新文本时,在输入文本的开头移动.
有没有办法在更改contenteditable值后恢复光标位置?或者也许有其他方法可以获得相同的效果?