我有一个文本区域,当用户选择并按 Enter 键时,我希望它更改为其他内容。例如我有一个文本区域,当用户选择像 NY 这样的东西时,我希望这两个字母更改为 NY
我有类似的东西:
$('textarea').html('I live in NY and it\'s a great place to live');
$(window).click(function(){
var selection = selectedText();
console.log(selection);
});
function selectedText() {
var ret = '';
if (window.getSelection){
ret = window.getSelection();
} else if (document.getSelection) {
ret = document.getSelection();
} else if (document.selection) {
ret = document.selection.createRange().text;
}
return ret;
}
我不知道在哪里检查它是来自 textarea 还是来自其他地方,而且我不知道如何更改 textarea 文本的特定部分