Question

I have a contenteditable div and use execCommand("indent") for the tab key. I discovered that it wraps the tabbed text inside a blockquote element, and when I hit backspace it doesn't remove the blockquote but it just goes to the previous line. Is there some sort of "outdent" command that I can use to remove the blockquote element? (JSFiddle; note: I also discovered that when I hit enter it just wraps the following text in a div tag. Can this be avoided? I'd rather have it add a <br> element but no browsers support the insertBrOnReturn command.)

Any help would be much appreciated.

HTML

<div class="myClass"></div>

Javascript

$(".myClass").prop("contenteditable", true);

$(".myClass").on("keydown", function(e) {
    var keyCode = e.keyCode || e.which;
    if( keyCode === 9 ) {
        document.execCommand("indent", true, null);
        e.preventDefault();
    }
});
Était-ce utile?

La solution

Yes there is an "outdent" command. I have updated your fiddle. Keep in mind that different browsers have different support for contentEditable. In Chrome the backspace will outdent your contentEditable by default, while in IE it wouldn't.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top