[英]How to make selected text stay selected in textarea?
I'm working on a WYSIWYG editor. 我正在从事所见即所得的编辑器。 I have a textarea and I can get the selected text. 我有一个textarea,可以获取所选的文本。 But when I click something outside of the textarea the text becomes unselected. 但是,当我单击文本区域之外的内容时,该文本将变为未选中状态。 I want to make it stay selected unless I select another portion of text or click somewhere in textarea. 我要使其保持选中状态,除非我选择文本的另一部分或单击文本区域中的某个位置。 How can I achieve that? 我该如何实现?
You can use something like this 你可以用这样的东西
at your HTML 用你的HTML
<form name="myform">
<input type="text" name="mytextfield">
</form>
and use method focus 和使用方法重点
<body OnLoad="document.myform.mytextfield.focus();">
(on)select save the start and end position of the selection to the input element itself. (on)select将选择的开始和结束位置保存到输入元素本身。
event.target.lastSelection = { start:startpos, end:endpos}; event.target.lastSelection = {start:startpos,end:endpos};
and use the (on)blur event to do something like 并使用(on)blur事件执行类似的操作
event.target. event.target。 setSelectionRange ( event.target.lastSelection.start, event.target.lastSelection.end ) setSelectionRange (event.target.lastSelection.start,event.target.lastSelection.end)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.