简体   繁体   中英

textarea trailing new lines when selected via keyboard vs mouse

If I initialize a textarea with 3 rows of text and 3 newlines like this

<textarea rows="10" cols="50">
A
B
C



</textarea>

When you highlight the text with a mouse the clipboard will contain all 3 new lines.
鼠标高亮

When you highlight the text with ctrl+a and copy the clipboard will contain an extra 4th new line.
键盘高亮显示

This is causing me some problems in a WYSIWYG editor that I'm working on. How would you make the copy paste behavior act the same no matter if the text was highlighted with a keyboard or a mouse

Ok. But where is problem? In both cases you have the same content in clipboard.

Open console and try this:

 function showCB() { let text = window.getSelection(); text = text.toString(); console.log(text.length); } document.addEventListener('mouseup', showCB); document.addEventListener('keyup', showCB); document.addEventListener('selectionchange', showCB);
 <textarea rows="10" cols="50"> AB C </textarea>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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