If I do “jquery sortable” on a contenteditable item(s), I then can't focus mouse anywhere inside contenteditable text anymore

后端 未结 7 2265
情深已故
情深已故 2021-02-07 05:31

Strangely this is broken only in Firefox and Opera (IE, Chrome and Safari works as it should).

Any suggestions for a quick fix?




        
相关标签:
7条回答
  • 2021-02-07 05:51

    Actually this is working, as you can see by pressing tab: the editable element receives focus. I think the problem is that the sortable plug-in is hijacking the mousedown event and thus preventing the editable element from receiving focus when you click on it.

    A workaround is to add a mousedown event handler to the editable element that ensures it receives focus:

    <html>
    <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js" type="text/javascript"></script>
      <script>
      $(document).ready(function(){
          $('#sortable').sortable();
          $('#editable')[0].onmousedown = function() {
              this.focus();
          };
      });
      </script>
    </head>
    <body>
      <span id="sortable">
        <p contenteditable="true" id="editable">One apple</p>
        <p>Two pears</p>
        <p>Three oranges</p>
      </span>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题