简体   繁体   English

添加文本后,将光标置于文本区域的末尾

[英]Put cursor at the end of textarea after adding text

I'm trying to make a button to give a comment on someone's profile page. 我正在尝试创建一个按钮,以便在某人的个人资料页面上发表评论。 The JavaScript now adds the '@username' to the text area so some kind of mention is made. JavaScript现在将“ @username”添加到文本区域,因此进行了某种提及。 This works very well. 这很好。 The only problem is, that when I try to focus to the text area after the mention has been added, the cursor is put in before of the text, and not after. 唯一的问题是,当我在添加提及内容后尝试将焦点移至文本区域时,光标将放置在文本之前,而不是之后。

<a style="cursor: pointer;" class="pull-right"onclick='document.getElementById("comment").value = "@username"; document.section.comment.focus();'>Click to comment</a>

And for the text area 而对于文本区域

<textarea id="comment" name="comment" class="form-control" type="text" rows="4" placeholder="Write comment here" ></textarea>

 <a style="cursor: pointer;" class="pull-right" onclick='document.getElementById("comment").value = "@username "; document.section.comment.focus();'>Click to comment</a> <br><br> <form action='' method="POST" name="section"> <textarea id="comment" name="comment" class="form-control" type="text" rows="4" placeholder="Write your comment" ></textarea><!-- <p id="reageer_op"></p> --> </form> 

I'd like to keep it as simple as possible. 我想让它尽可能简单。

Is there a solution without jQuery? 有没有jQuery的解决方案吗? I found this solution with jQuery here https://css-tricks.com/snippets/jquery/mover-cursor-to-end-of-textarea/ 我在这里用jQuery找到了这个解决方案https://css-tricks.com/snippets/jquery/mover-cursor-to-end-of-textarea/

I've found the solution myself, add the following to the script of the button 我自己找到了解决方案,将以下内容添加到按钮的脚本中

document.section.comment.setSelectionRange(document.section.comment.value.length,document.section.comment.value.length);

 <a style="cursor: pointer;" class="pull-right" onclick='document.getElementById("comment").value = "@username "; document.section.comment.focus();document.section.comment.setSelectionRange(document.section.comment.value.length,document.section.comment.value.length);'>Click to comment</a> <br><br> <form action='' method="POST" name="section"> <textarea id="comment" name="comment" class="form-control" type="text" rows="4" placeholder="Write your comment" ></textarea><!-- <p id="reageer_op"></p> --> </form> 

This did the trick! 这成功了! Thanks anyways! 不管怎么说,多谢拉!

停止在其中添加文本<textarea>&lt;i&gt;after condition fulfill&lt;/div&gt;&lt;/i&gt;&lt;b&gt;条件满足后&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我有一个文本区域</p><pre>&lt;textarea rows="3" [(ngModel)]="formatObj.textAreaBox" (keyup)="onExtendexDelimeterKeyUp($event)" #extendDelimiters&gt;&lt;/textarea&gt;</pre><p> 我想在条件满足后停止在文本区域中添加文本。 我不想禁用 textarea,因为用户可以删除 textarea 中的文本。 我想停止使用 Angular 7 执行 keyup</p><p> 谢谢</p></div> - Stop adding text in <textarea> after condition fulfill

暂无
暂无

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

相关问题 tinymce 4如何将光标放到文本的末尾 - tinymce 4 how to put cursor to end of the text 将 cursor 放在文本输入值的末尾 - put cursor at end of text input's value 如何将输入框 cursor 放在文本末尾 - how to put input box cursor at end of text 如何在文本区域的光标开始和结束之间获取文本 - how to get text in between cursor start and end of textarea 停止在其中添加文本<textarea>&lt;i&gt;after condition fulfill&lt;/div&gt;&lt;/i&gt;&lt;b&gt;条件满足后&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我有一个文本区域</p><pre>&lt;textarea rows="3" [(ngModel)]="formatObj.textAreaBox" (keyup)="onExtendexDelimeterKeyUp($event)" #extendDelimiters&gt;&lt;/textarea&gt;</pre><p> 我想在条件满足后停止在文本区域中添加文本。 我不想禁用 textarea,因为用户可以删除 textarea 中的文本。 我想停止使用 Angular 7 执行 keyup</p><p> 谢谢</p></div> - Stop adding text in <textarea> after condition fulfill 在HTML textArea中的每个文本后添加空格 - Adding a space after each text in a HTML textArea 如何在文本区域的末尾设置光标? - How to set cursor at the end in a textarea? 文本结尾后如何放置冒号 - How to put colon after the end of the text 在 contenteditable 中添加 span 标签后 cursor 移动到 IOS safari 结尾 - After adding span tag in contenteditable cursor move to end in IOS safari 使用Javascript在textarea中的光标处插入文本 - Inserting text at cursor in a textarea, with Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM