在JavaScript中使用execcommand函数时,可以删除第一个字符吗?

问题描述

我可以使用JavaScript中的execCommand函数删除第一个字符吗?

所以,我需要:

"My string..."
document.execCommand('...');
"y string..."

我有代码;

$('#myDiv').keydown(function(e) {
    var sel = document.getSelection(),nd = sel.anchorNode,text = nd.textContent.slice(0,sel.focusOffset),col = text.split("\n").pop();
    switch (e.keyCode) {
        case 13:
            if (e.shiftKey) {
                // ....
            }
            break;
    }
});

谢谢。

解决方法

要删除字符串的第一个字符,可以使用substring(1)函数:

let str = "My string...";
let res = str.substring(1);
console.log(res);

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...