如何替换html textarea中当前选定的文本? [英] How to replace the currently selected text inside an html textarea?
本文介绍了如何替换html textarea中当前选定的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何编辑textarea表单元素的选定文本?
解决方案
这样做:
< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/ jquery.min.js>< / script>< textarea> Hello world。< / textarea><按钮>以乐趣替换< / code>
How do I edit the selected text of a textarea form element?
EDIT: as in edit it in-place, replacing the orignal text.
解决方案
This works:
function replaceIt(txtarea, newtxt) {
$(txtarea).val(
$(txtarea).val().substring(0, txtarea.selectionStart)+
newtxt+
$(txtarea).val().substring(txtarea.selectionEnd)
);
}
$("button").on('click', function() {
replaceIt($('textarea')[0], 'fun')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>Hello world.</textarea>
<button>Replace with fun</button>
这篇关于如何替换html textarea中当前选定的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文