交换后jQuery可拖动项目失去了可拖动性(使用jsfiddle示例)

前端之家收集整理的这篇文章主要介绍了交换后jQuery可拖动项目失去了可拖动性(使用jsfiddle示例)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个li元素,它们是jQuery draggable.当我将盒子’one’拖放到方框’two’上时,它们会被交换掉.到现在为止还挺好. (延迟修复了 here中描述的另一个问题.)然而,即使重置了可拖动选项,元素现在也不再可拖动了.

任何想法如何解决这一问题? full working jsfiddle here

  1. <html>
  2. <head>
  3.  
  4. <script type="text/javascript" src="includes/jquery-1.4.2.min.js"></script>
  5.  
  6. <script type="text/javascript" src="includes/jquery-ui-1.8.2.custom.min.js"></script>
  7.  
  8. <script type="text/javascript">
  9.  
  10. jQuery.fn.swapWith = function(to) {
  11. return this.each(function() {
  12. var copy_to = $(to).clone(true);
  13. var copy_from = $(this).clone(true);
  14. $(to).replaceWith(copy_from);
  15. $(this).replaceWith(copy_to);
  16. });
  17. };
  18.  
  19.  
  20.  
  21. $(document).ready(function() {
  22.  
  23. options = { revert: true};
  24.  
  25. $("li").draggable(options);
  26.  
  27. $('#wrapper').droppable({
  28. drop: function(event,ui) {
  29. window.setTimeout("Swap()",600);
  30. }
  31. });
  32. });
  33.  
  34. function Swap() {
  35. $('#one').swapWith($('#two'));
  36.  
  37. //trying to fix problem where elements can't be dragged anymore
  38. $("li").draggable("destroy");
  39. $("li").draggable(options);
  40. }
  41. </script>
  42.  
  43. </head>
  44. <body>
  45. <form>
  46. <ul id="wrapper">
  47. <li id='one'>
  48. <div style="width: 100px; height: 100px; border: 1px solid green">
  49. one<br /></div>
  50. </li>
  51. <li id='two'>
  52. <div style="width: 110px; height: 110px; border: 1px solid red">
  53. two<br /></div>
  54. </li>
  55. </ul>
  56. <br />
  57. </form>
  58. </body>
  59. </html>

解决方法

所以在玩了你的代码之后,这是我得出的结论.

看起来可拖动的jqueryui方法正在单独跟踪其对象.克隆时,不会克隆该跟踪.我修改了你的代码如下:

  1. jQuery.fn.swapWith = function(to) {
  2. return this.each(function() {
  3. var copy_to = $(to).clone(true).appendTo($("#wrapper"));
  4. var copy_from = $(this).clone(true).appendTo($("#wrapper"));
  5. //$(to).replaceWith(copy_from);
  6. //$(this).replaceWith(copy_to);
  7. });
  8. };

你可以看到http://jsfiddle.net/XkUDv/16/的迷人结果

如您所见,如果拖动新的克隆对象,它将移动原始对象而不是克隆对象.

这不是答案,但我希望它有所帮助.

更新:

仔细看看克隆后我将javascript更改为:

  1. <script type="text/javascript">
  2.  
  3. jQuery.fn.swapWith = function(to) {
  4. return this.each(function() {
  5. var copy_to = $(to).clone();
  6. var copy_from = $(this).clone();
  7. $(to).replaceWith(copy_from);
  8. $(this).replaceWith(copy_to);
  9. });
  10. };
  11.  
  12.  
  13.  
  14. $(document).ready(function() {
  15.  
  16. options = { revert: true };
  17.  
  18. $("li").draggable(options);
  19. $('#wrapper').droppable({
  20. drop: function(event,ui) {
  21. window.setTimeout(function(){
  22. $('#one').swapWith($('#two'));
  23. $("li").draggable(options);
  24. },600);
  25. }
  26. });
  27. });
  28. </script>

现在它按照你想要的方式工作:)

我猜测问题是,因为clone(true)复制事件处理程序,当您尝试将draggable重新附加到新克隆时,它会看到旧的事件处理程序并忽略这些元素.所以我没有克隆(true),而是将其更改为clone();

希望有所帮助!

工作版本:http://jsfiddle.net/XkUDv/21/

猜你在找的jQuery相关文章