且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

防止Firefox在用户点击返回时创建新的可用div

更新时间:2023-12-01 22:19:16

Ok I kinda figured it out now.

It's not the best (read cleanest) solution, but it works for me.

Only have tested this on Chrome, but I think other browsers will work the same.

I ended up doing this:

  $('div[contenteditable=true]').keydown(function(e) {
    // trap the return key being pressed
    if (e.keyCode == 13) {
      // insert 2 br tags (if only one br tag is inserted the cursor won't go to the second line)
      document.execCommand('insertHTML', false, '<br><br>');
      // prevent the default behaviour of return key pressed
      return false;
    }
  });

相关阅读

推荐文章