简体   繁体   English

允许使用execCommand在contentEditable div中进行Tabbing

[英]Allow Tabbing in a contentEditable div with execCommand

I am making an application where I need for a contentEditable div to be allowed to have tabs in it. 我正在创建一个应用程序,我需要一个contentEditable div允许其中有选项卡。 I have already figured out that it is really not possible to have to work correctly. 我已经发现,实际上不可能正常工作。 So is there a way that on keyDown it adds the HTML code for a tab, which is 那么有没有办法在keyDown上添加标签的HTML代码,即

	

What I have so far is this 到目前为止,我有这个

document.getElementById('codeline').contentEditable='true';
document.getElementById('codeline').onkeydown=function(e){
    if(e.keyCode==9){
        e.preventDefault();
        //document.getElementById('codeline').contentWindow.document.execCommand("InsertHTML",false,"	"); 
        //Thought this would work but it doesn't
    }
}

If anybody knows if there is a way to do this, or if that is the way and I am simply doing it wrong, please tell me! 如果有人知道是否有办法做到这一点,或者如果是这样,我只是做错了,请告诉我! Thanks! 谢谢!

For future readers, perhaps a simpler solution is to just use the 'pre' white-space style. 对于未来的读者来说,也许更简单的解决方案就是使用'pre'白色空间风格。 original post here . 原帖在这里

white-space: pre;

The HTML spec specifies that a TAB char should be treated as a single white space except for when it's contained inside a <pre> element. HTML规范指定应将TAB char视为单个空格,除非它包含在<pre>元素中。

The code that you posted above does work and it does insert a TAB char but it's just displayed by the browser as a white space. 您在上面发布的代码确实有效,它确实插入了TAB字符,但它只是由浏览器显示为空格。 If you can put your entire editable content in a <pre> tag then your tabs will show up. 如果您可以将整个可修改内容放在<pre>标记中,那么您的标签就会显示出来。

If you only want to use the tabs to indent content, you can also look into 如果您只想使用选项卡缩进内容,您还可以查看

execCommand('indent', false, null)

"indent" command, firefox wraps the selected text with blockquote element. “indent”命令, firefox用blockquote元素包装所选文本。
So one can define a margin-left or padding-left property for blockquote element to represent tabbing. 因此,可以为blockquote元素定义margin-leftpadding-left属性来表示tabbing。
The "outdent" command will remove the blockquote element and hence the styles. “outdent”命令将删除blockquote元素,从而删除样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM