Javascript 使用计算机键盘按字而不是按字符删除
我的网站上有一个文本区。我有一句话:Javascript 使用计算机键盘按字而不是按字符删除,javascript,jquery,html,css,web,Javascript,Jquery,Html,Css,Web,我的网站上有一个文本区。我有一句话: (e.g.) sampleword 我想逐字删除,而不是按字符删除 Output: '' Incorrect Output: ampleword 更新1: 下面是我的文本区域的图像: 我想使用键盘上的delete按钮删除整个单词 输出不正确: 正确输出: 我该怎么做?我应该使用什么技术?您可以使用 如示例中所示: var str = 'Twas the night before Xmas...'; var newstr =
(e.g.)
sampleword
我想逐字删除,而不是按字符删除
Output:
''
Incorrect Output:
ampleword
更新1:
下面是我的文本区域的图像:
我想使用键盘上的delete
按钮删除整个单词
输出不正确:
正确输出:
我该怎么做?我应该使用什么技术?您可以使用
如示例中所示:
var str = 'Twas the night before Xmas...';
var newstr = str.replace(/xmas/i, 'Christmas');
console.log(newstr); // Twas the night before Christmas...
所以你可以做:
var str = 'sampleword';
var newstr = str.replace('sampleword', '');
并获得您的预期输出。这对我来说一直都很有效 请尝试以下操作:
var string = 'This is a word.';
var replacedstring = string.replace('word', 'sentence');
alert(replacedstring);
在这个例子中,我使用了string.replace('word','句子')代码>用于替换单词
在字符串中,这是一个单词。
word是将被句子
替换的单词
对于jquery,请使用以下命令:
$("#string").val($("#stript").val().replace('word', 'sentence'));
您不一定需要jQuery来完成这一点。常规JavaScript也足够了:
甚至
document.getElementById("txtArea").innerHTML = document.getElementById("txtArea").value.replace('sampleword', '');
在jQuery中:
document.getElementById("txtArea").innerHTML = document.getElementById("txtArea").value.replace('sampleword', '');
$("#txtArea").val($("#txtArea").val().replace('sampleword', ''));