简体   繁体   English

JavaScript克隆不起作用

[英]JavaScript clone doesn't work

Look this jsfiddle https://jsfiddle.net/3o38nbzs/4/ when I clone, I can move the clone, but it does not "listen" click. 看看这个jsfiddle https://jsfiddle.net/3o38nbzs/4/当我克隆时,我可以移动克隆,但它不会“听”点击。 https://jsfiddle.net/L5mg87jm/ when I clone, the clone is static. https://jsfiddle.net/L5mg87jm/克隆时,克隆是静态的。

At first I used Clone () and the second clone (true, true), that is the only difference. 起初我使用Clone()和第二个克隆(true,true),这是唯一的区别。

How do I create a clone to respond to the click event? 如何创建克隆以响应click事件?

Basically, I have trouble knowing when the User clicks on a cloned object. 基本上,我很难知道用户何时点击克隆对象。 I tried using clone (true, true), but it still fails. 我尝试使用clone(true,true),但它仍然失败。 When I use .clone() I can move drag the object, but the click doesn't work. 当我使用.clone()时,我可以移动拖动对象,但点击不起作用。 When I use .clone(true,true) nothing works. 当我使用.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() is shallow copy. clone()shallow拷贝。 Normally, any event handlers bound to the original element are not copied to the clone. 通常,绑定到原始元素的任何事件处理程序都不会复制到克隆。

You must use clone(true) which will copy all the events as well. 您必须使用clone(true) ,它也将复制所有事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM