[英]Add Text to TextArea Where Cursor is currently in position Trigger By Shortcut Key?
I have to two textarea
each with same button
class .addText
. 我必须在两个textarea
分别使用相同的button
类.addText
。 I am trying insert word hello world
to the textarea
where the cursor in, this event would trigger via shortcut key ctrl
+ i
. 我正在尝试将单词hello world
插入光标所在的textarea
,此事件将通过快捷键ctrl
+ i
触发。
$(document).ready(function() { $(window).keydown(function(event) { console.log(event.keyCode); if (event.ctrlKey && event.keyCode == 73) { $(".addText").trigger("click"); event.preventDefault(); } }) $('.addText').click(function() { // Add Text 'hello world' to input textarea that curssor currently in $(this).next().val('hello world'); }) });
<!-- Include BS and FA --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <div class="container"> <div class="row"> <div class="col-xs-6"> <div class="ta1"> <button class="addText">Add Text</button> <textarea cols="30" rows="10" class="inputTa1"></textarea> </div> </div> <div class="col-xs-6"> <div class="ta1"> <button class="addText">Add Text</button> <textarea cols="30" rows="10" class="inputTa2"></textarea> </div> </div> </div> </div>
However, Currently, the word hello world
is inserted into both textarea when shortcut key ctrl
+ i
is pressed. 但是,当前,按下快捷键ctrl
+ i
时,单词hello world
会插入两个文本区域。
I could not differentiate which textarea cursor is currently in, I found one source here: Insert text into textarea at cursor position (Javascript) , but it does not apply to my case. 我无法区分当前所在的textarea光标,在这里找到了一个来源: 将文本插入到光标位置处的textarea(Javascript)中 ,但这不适用于我的情况。
How can I check which textarea that has cursor in and apply an action to it? 如何检查光标所在的文本区域并对其执行操作? Thanks. 谢谢。
You could just do the reverse of your button click. 您可以执行与按钮单击相反的操作。
Instead of doing next
, do prev
on the target element. 代替执行next
,而是对目标元素执行prev
。
eg. 例如。
$(document).ready(function() { $(window).keydown(function(event) { console.log(event.keyCode); if (event.ctrlKey && event.keyCode == 73) { event.preventDefault(); $(event.target).prev().trigger("click"); } }) $('.addText').click(function() { // Add Text 'hello world' to input textarea that curssor currently in $(this).next().val('hello world'); }) });
<!-- Include BS and FA --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <div class="container"> <div class="row"> <div class="col-xs-6"> <div class="ta1"> <button class="addText">Add Text</button> <textarea cols="30" rows="10" class="inputTa1"></textarea> </div> </div> <div class="col-xs-6"> <div class="ta1"> <button class="addText">Add Text</button> <textarea cols="30" rows="10" class="inputTa2"></textarea> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.