简体   繁体   English

如何让 textarea 的 cursor 在拖放时移动到突出显示的文本的末尾?

[英]How to get textarea's cursor move to the end of the highlighted text on drag and drop?

When I drag and drop the image into the textarea, the text is highlighted.当我将图像拖放到文本区域时,文本会突出显示。
Is there a way to have the textarea's cursor move to the end of the highlighted text?有没有办法让 textarea 的 cursor 移动到突出显示的文本的末尾?

 document.querySelectorAll('.emoteImage').forEach((emoteImage) => { emoteImage.addEventListener('dragstart', (event) => { event.dataTransfer.setData('text', emoteImage.alt); event.effectAllowed = "copy"; }); });
 img { width: 100px; }
 <div class="emote"> <img src="https://images.duckduckgo.com/iu/?u=http%3A%2F%2Ficons.iconarchive.com%2Ficons%2Fpaomedia%2Fsmall-n-flat%2F512%2Fcat-icon.png&f=1" alt="emote1" title="emote1" class="emoteImage"> </div> <textarea></textarea>

i added a "dragEnd" event to move the cursor to the end of the highlighted text.我添加了一个“dragEnd”事件将 cursor 移动到突出显示的文本的末尾。 Hope it helps:)希望能帮助到你:)

 var element = document.getElementById("textArea"); document.querySelectorAll('.emoteImage').forEach((emoteImage) => { emoteImage.addEventListener('dragstart', (event) => { event.dataTransfer.setData('text', emoteImage.alt); event.effectAllowed = "copy"; }); emoteImage.addEventListener( 'dragend', function( event ) { var end = element.selectionEnd; element.focus(); element.setSelectionRange(end,end); }); });
 img { width: 100px; }
 <div class="emote"> <img src="https://images.duckduckgo.com/iu/?u=http%3A%2F%2Ficons.iconarchive.com%2Ficons%2Fpaomedia%2Fsmall-n-flat%2F512%2Fcat-icon.png&f=1" alt="emote1" title="emote1" class="emoteImage"> </div> <textarea id="textArea" ></textarea>

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

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