2015-02-17 108 views
0

看看這個jsfiddle https://jsfiddle.net/3o38nbzs/4/當我克隆時,我可以移動克隆,但它不會「聽」點擊。 https://jsfiddle.net/L5mg87jm/當我克隆時,克隆是靜態的。JavaScript克隆不工作

起初,我使用了Clone()和第二個克隆(true,true),這是唯一的區別。

如何創建克隆以響應點擊事件?

基本上,我很難知道用戶何時單擊克隆的對象。我嘗試使用克隆(true,true),但仍然失敗。 當我使用.clone()我可以移動拖動對象,但點擊不起作用。 當我使用.clone(true,true)什麼都不起作用。

 // Clone Block 
     function cloneblock(obj, event, ui) { 
      var idn = Math.floor((Math.random() * 100) + 1), idt = '#' + idn; 
      if (obj) { 
       var block = $(selected).clone(); 
       block.css({ left: mouse.X + 'px', top: mouse.Y + 'px' }).attr('id', idn).removeClass('ui-draggable selected'); 
      } else { 
       var block = $(ui.draggable).clone(); 
       block.css({ left: mouse.X + 'px', top: mouse.Y + 'px' }).attr('id', idn).removeClass('ui-draggable selected gblock').addClass('block'); 
      } 
      block.children('.ui-widget-content').children('p').children('.vertexout').attr('id', 'vo' + idn); 
      var exit = block.children('.ui-widget-content').children('p').children('.vertexout'); 
      if ($(exit).hasClass('image')) { 
       $(exit).attr('id', 'v' + idn + 4); 
       jsPlumb.makeSource($(exit), { 
        scope: 'image' 
       }); 
      } else if ($(exit).hasClass('int')) { 
       $(exit).attr('id', 'v' + idn + 5); 
       jsPlumb.makeSource($(exit), { 
        scope: 'int' 
       }); 
      } else if ($(exit).hasClass('float')) { 
       $(exit).attr('id', 'v' + idn + 6); 
       jsPlumb.makeSource($(exit), { 
        scope: 'float' 
       }); 
      } else { 
       $(exit).attr('id', 'v' + idn + 7); 
       jsPlumb.makeSource($(exit), { 
        scope: 'char' 
       }); 
      } 
      var vin2 = block.children('.ui-widget-content').children('p').children('.vertexin'); 
      for (i = 0; i < vin2.length; i++) { 
       if ($(vin2[i]).hasClass('image')) { 
        $(vin2[i]).attr('id', 'v' + idn); 
        jsPlumb.makeTarget($(vin2[i]), { 
         maxConnections: 1, 
         scope: 'image' 
        }); 
       } else if ($(vin2[i]).hasClass('int')) { 
        $(vin2[i]).attr('id', 'v' + idn + 1); 
        jsPlumb.makeTarget($(vin2[i]), { 
         maxConnections: 1, 
         scope: 'int' 
        }); 
       } else if ($(vin2[i]).hasClass('float')) { 
        $(vin2[i]).attr('id', 'v' + idn + 2); 
        jsPlumb.makeTarget($(vin2[i]), { 
         maxConnections: 1, 
         scope: 'float' 
        }); 
       } else { 
        $(vin2[i]).attr('id', 'v' + idn + 3); 
        jsPlumb.makeTarget($(vin2[i]), { 
         maxConnections: 1, 
         scope: 'char' 
        }); 
       } 
      } 
      block.appendTo($(container)); 
      jsPlumb.draggable($(idt), dragop); 
      resize(); 
     } 
+7

這是*很多代碼。你能否把它縮小到與重新創建問題相關的部分。 – 2015-02-17 20:44:59

+0

並且,請修復您的代碼以正確縮進,因爲它不是非常可讀的。你總是可以使用像http://jsbeautifier.org/這樣的東西來修復縮進。 – jfriend00 2015-02-17 21:09:10

回答

0
var block = $(selected).clone(); 

clone()shallow副本。通常情況下,綁定到原始元素的任何事件處理程序都不會複製到克隆中。

您必須使用clone(true)它也會複製所有事件。

+0

我知道,用克隆(true)和克隆()用一個例子。 正如我上面所說的,當使用clone()https://jsfiddle.net/3o38nbzs/4/時,拖動工作時使用克隆(true)https://jsfiddle.net/L5mg87jm/拖動不起作用。 thx – Bollado 2015-03-05 11:31:57