[英]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! 不管怎么说,多谢拉!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.