gpt4 book ai didi

javascript - 添加文本到文本区域 光标当前所在位置 通过快捷键触发?

转载 作者:行者123 更新时间:2023-12-01 02:27:59 26 4
gpt4 key购买 nike

我有两个 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>

但是,目前,当按下快捷键 ctrl + i 时,单词 hello world 会插入到两个文本区域中。

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

如何检查光标所在的文本区域并对其应用操作?谢谢。

最佳答案

您可以执行与单击按钮相反的操作。

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

例如。

$(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>

关于javascript - 添加文本到文本区域 光标当前所在位置 通过快捷键触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48581781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com