【问题标题】:Chrome execCommand returns errorChrome execCommand 返回错误
【发布时间】:2011-12-03 18:58:53
【问题描述】:

如何在 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");

        }
    }
}

【问题讨论】:

标签: javascript google-chrome getelementbyid execcommand dom


【解决方案1】:

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

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

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

【讨论】:

  • 我明白你的意思,我只是不知道如何正确处理它。我觉得我所拥有的应该工作,但它只是......没有。此外,在除 chrome 之外的所有其他浏览器中,如果我将 contentEditable 区域设置为 pre,则选项卡可以在没有 JavaScript 的情况下工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 2012-07-01
  • 2012-02-03
  • 2020-02-27
  • 1970-01-01
相关资源
最近更新 更多