JavaScript克隆不工作
问题描述:
看看这个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();
}
答
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
这是*很多代码。你能否把它缩小到与重新创建问题相关的部分。 – 2015-02-17 20:44:59
并且,请修复您的代码以正确缩进,因为它不是非常可读的。你总是可以使用像http://jsbeautifier.org/这样的东西来修复缩进。 – jfriend00 2015-02-17 21:09:10