如何在 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()
作为其 TextRange
和 ControlRange
对象的方法,但这些在其他浏览器中不存在。
document.execCommand("InsertHtml", false, "p");
你可能想考虑如果用户在之前选择了一些文本时按下 Tab 键会发生什么:在这种情况下你可能想要 delete the contents of the selection在插入制表符之前。
关于javascript - Chrome execCommand 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7700848/