javascript - Chrome execCommand 返回错误

标签 javascript google-chrome getelementbyid execcommand dom

如何在 Chrome 中使用 execCommand()?这是我现在拥有的代码 它被用来在点击标签按钮时插入一个特殊字符

function editAble(supr){
    document.getElementById('codeline').contentEditable='true';
    document.getElementById('codeline').onkeydown=function(e)
        {

        if(e.keyCode==9){
            e.preventDefault();
            range1 = document.getElementById('codeline');
            range1.execCommand("InsertHtml",false,"p");

        }
    }
}

最佳答案

execCommand() 方法是Document 对象而非元素的方法。 IE 还提供 execCommand() 作为其 TextRangeControlRange 对象的方法,但这些在其他浏览器中不存在。

document.execCommand("InsertHtml", false, "p");

你可能想考虑如果用户在之前选择了一些文本时按下 Tab 键会发生什么:在这种情况下你可能想要 delete the contents of the selection在插入制表符之前。

关于javascript - Chrome execCommand 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7700848/

相关文章:

javascript - 在 chrome 扩展中添加多个外部域

javascript - 使元素在 IE 中可见?

javascript - 根据 comboBox 值启用/禁用 txtBox

javascript - html Javascript 通过 onmouseover 在 <a> 按钮上 move 图像

javascript - 使用canvas + JavaScript将云(图像)插入落雪的最简单方法

javascript - 更改数组的值更改原始数组 JavaScript

javascript - PHP 下拉列表所选值未插入数据库

javascript - 使用jquery捕获对象标签子事件

javascript - 在 chrome.browserAction.onClicked 上调用内容脚本函数

javascript - Chrome for 循环优化