简体   繁体   English

如何选择文本区域中的部分文本并将其复制到剪贴板?

[英]How do you select portion of text in textarea and copy it to clipboard?

Could anyone tell how to select portion of text in textarea and copy it to clipboard by using javascript? 谁能说出如何选择文本区域中的一部分文本并使用javascript将其复制到剪贴板吗? I know how to select all text in textarea and copy it? 我知道如何选择文本区域中的所有文本并将其复制吗? My question is that when we use mouse to select part of text in textarea, how to copy it to clipboard. 我的问题是,当我们使用鼠标选择文本区域中的一部分文本时,如何将其复制到剪贴板。

How about this 这个怎么样

(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.setSelectionRange( event.target.lastSelection.start, event.target.lastSelection.end )? event.target.setSelectionRange(event.target.lastSelection.start,event.target.lastSelection.end)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM