简体   繁体   English

jQuery UI可调整大小的克隆元素(.clone(true))不会调整大小

[英]jQuery UI resizable cloned element(.clone(true)) doesn't resize

I'm having this strange problem with cloned elements(using .clone(true)) with draggable and resizable functionalities from jQuery UI. 我在使用jQuery UI中具有可拖动和可调整大小功能的克隆元素(使用.clone(true))时遇到了这个奇怪的问题。 After cloning, the cloned elements don't have these functionalities, they just don't work. 克隆后,克隆的元素不具有这些功能,它们只是不起作用。

I have been looking for various solutions, like assigning the functionalities after cloning and still having the problem. 我一直在寻找各种解决方案,例如在克隆后分配功能并仍然遇到问题。

Here is the code 这是代码

jQuery(document).ready(function(){
            jQuery('#res').draggable({
                containment: 'body',
                grid: [ 10, 10 ],
                snap: true,
            });
            jQuery('#res').resizable({
                grid : 10,
                handles : 's'
            });
            var res_clone = jQuery('#res').clone(true);
            jQuery(res_clone).attr('id', 'res_clone');
            jQuery('#res').parent().append(res_clone);
        });

I've found a solution to my problem. 我已经找到解决问题的办法。 Using .clone(true) on resizable elements, the event handlers does not seem to work so instead I do a simple .clone(). 在可调整大小的元素上使用.clone(true),事件处理程序似乎不起作用,因此我改用了一个简单的.clone()。 Now, the cloned element contains the .ui-resizable-handler divs that interfere with the newly added handlers by the .draggable() method, thus persisting the problem, so before applying the .draggable() method I've stripped all the .ui-resizable-handler divs found in the cloned element. 现在,克隆的元素包含.ui-resizable-handler div,它们通过.draggable()方法干扰新添加的处理程序,因此仍然存在该问题,因此在应用.draggable()方法之前,我已经剥离了所有。在克隆的元素中找到ui-resizable-handler div。

The draggable functionality works without any problems. 可拖动的功能可以正常工作。

Working example 工作实例

jQuery(document).ready(function(){
            jQuery('#res').draggable({
                containment: 'body',
                grid: [ 10, 10 ],
                snap: true,
            });
            jQuery('#res').resizable({
                grid : 10,
                handles : 's'
            });

            var res_clone = jQuery('#res').clone();
            jQuery(res_clone).attr('id', 'res_clone');
            jQuery(res_clone).find('.ui-resizable-handle').remove();
            jQuery(res_clone).draggable({
                containment: 'body',
                grid: [ 10, 10 ],
                snap: true,
            });
            jQuery(res_clone).resizable({
                grid : 10,
                handles : 's'
            });

            jQuery('#res').parent().append(res_clone);
        });

JQuery.clone() will only work for DOM. JQuery.clone()仅适用于DOM。 So you can make it resizable or draggable, only after you append it to parent. 因此,只有将其附加到父级之后,才能使其可调整大小或可拖动。

See docs: http://api.jquery.com/clone/ 参见文档: http : //api.jquery.com/clone/

For JavaScript object use extend() 对于JavaScript对象,请使用extend()

http://api.jquery.com/jQuery.extend/ http://api.jquery.com/jQuery.extend/

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

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