将文本添加到TextArea光标当前位于何处触发按快捷键?

时间:2018-02-02 11:31:11

标签: javascript jquery html

我必须有两个textarea,每个button.addText。我正在尝试将单词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>
&#13;
&#13;
&#13;

但是,目前,当按下快捷键hello world + ctrl时,单词i会插入到textarea中。

我无法区分当前哪个textarea光标,我在这里找到了一个来源:Insert text into textarea at cursor position (Javascript),但它不适用于我的情况。

如何检查哪个textarea有光标并对其应用操作?感谢。

1 个答案:

答案 0 :(得分:2)

您可以按下按钮的反向。

而不是next,而是对目标元素执行prev

例如

&#13;
&#13;
$(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');
  })
});
&#13;
<!-- 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>
&#13;
&#13;
&#13;