Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用计算机键盘按字而不是按字符删除_Javascript_Jquery_Html_Css_Web - Fatal编程技术网

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', ''));