开发者

Div contenteditable and pressing "enter" on FF

I have got a div with contenteditable that should trigger a < br /> insert on "enter". Everything works fine in IE but Firefox drives me crazy.

this.e.keydown(function(event) {
  if(event.keyCode == 13) {
    var execute = editor.insertHTML(\'<br />\')';
    eval(execute);
    return false;
  } 
});

Firefox ignores < br /> at the end of the div and I guess at the beginning too. Means, if I press "enter" at the middle of a sentence, it works as it should. Trying the same at the end of a sentence (the very last one) it fails.

Any ideas? The same problem we have at the stackoverflow editor preview :-)

Press "enter" at the end > fail ... 开发者_JS百科press "enter" a single letter earlier > newline


Add this function instead

this.e.keydown(function(event) {
    if (event.keyCode == 13 || event.charCode == 13) {
        var execute = editor.insertHTML(\'<br />\')';
        eval(execute);
        return false;
    } 
});

FF reads charCode instead of keyCode.


You can try inserting <br /><span></span>. This moves the cursor to the next line but doesn't properly resize the container so it isn't perfect.


contentEditable - Firefox <br /> tag check here, it might give an idea :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜