There is html like this:
<div contenteditable="true" class="value research-form thumbnail">
Some text here
</div>
And content of div should dynamicly highlight some words while user types e.g make something like:
<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>
But there is a problem: when I refresh text in div cursor moves at start of the text in input.
Is there any ways to restore cursor position after changing contenteditable value? Or maybe there is other way to get the same effect?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…