If I do “jquery sortable” on a contenteditable ite

2019-03-18 01:34发布

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

Any suggestions for a quick fix?

<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot; type="text/javascript"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js&quot; type="text/javascript"></script>
  <script>
  $(document).ready(function(){
      $('#sortable').sortable();
  });
  </script>
</head>
<body>
  <span id="sortable">
    <p contenteditable="true">One apple</p>
    <p>Two pears</p>
    <p>Three oranges</p>
  </span>
</body>
</html>

7条回答
SAY GOODBYE
2楼-- · 2019-03-18 02:13

I had a similar issue, but it actually happened due to "disableSelection" call on an element.

查看更多
Summer. ? 凉城
3楼-- · 2019-03-18 02:14

I have found another "solution" to this problem.

When declaring your sortable, you should add a cancel: option like this:

<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({cancel: ':input,button,.contenteditable'});
  });
  </script>
</head>
<body>
  <span id="sortable">
    <p contenteditable="true" class="contenteditable">One apple</p>
    <p>Two pears</p>
    <p>Three oranges</p>
  </span>
</body>
</html>

This version does not force your cursor at the start, but you also can't drag using this field. I'd advice wrapping the <p> in a container which has some sort of dragging handle (like the dots at the start of every row in gmail).

side remark: ':input,button' is required, because that is the default option. If you do not add these, you get similar problems with inputfields and buttons.

查看更多
相关推荐>>
4楼-- · 2019-03-18 02:14

I had the same problem and it was because I was using the function disableSelection() together sortable()

查看更多
爷、活的狠高调
5楼-- · 2019-03-18 02:22

The Patrigan answer is correct, but there's a slightly better way to specify the cancel, such as:

{cancel: 'input,textarea,button,select,option,[contenteditable]'}
查看更多
ら.Afraid
6楼-- · 2019-03-18 02:28

Yes so for me it was the combination of adding:

cancel: 'input,textarea,button,select,option,[contenteditable]'

and then taking off the .disableSelection();

so i'm left with:

$("#sortable_list").sortable({
    cancel: 'input,textarea,button,select,option,[contenteditable]'
});
查看更多
Summer. ? 凉城
7楼-- · 2019-03-18 02:29

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>
查看更多
登录 后发表回答