简体   繁体   English

可调整大小的jQuery UI在动态元素上不起作用

[英]jQuery UI resizable does not work on dynamic elements

I'm storing a few resizable and draggable div tags in my database through admin dashboard and outputting them on my main website. 我正在通过管理仪表板在数据库中存储一些resizabledraggable div标签,并将其输出到我的主网站上。 But the problem is, resizable does not work but I managed to make draggable work. 但是问题是,无法resizable ,但是我设法进行了draggable工作。

Here's my DOM coming from database: 这是我的DOM来自数据库:

<div data-type="input" data-card="serial_no" data-title="Serial No" class="card-data card-text card-content serial_no serial_no_content ui-draggable ui-draggable-handle ui-resizable"><span>Content for Serial No</span>
  <div class="ui-resizable-handle ui-resizable-n" style="z-index: 90;"></div>
  <div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div>
  <div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div>
  <div class="ui-resizable-handle ui-resizable-w" style="z-index: 90;"></div>
</div>

NOTE: I wrapped above DOM in a div tag with a class .card 注意:我在DOM上方将div包裹在带有类.card的div标签中。

You can see ui-resizable-handle and ui-resizable-* classes are already there and I tried the following after outputting the DOM in a wrapper having a class name .card . 您可以看到ui-resizable-handleui-resizable-*类已经在那里,在具有类名.card的包装器中输出DOM之后,我尝试了以下.card

$(function(){

   /**
    * Searching for .card children with the class name .ui-resizable-handle
    * and removing them
    **/
   $('.card').find('.ui-resizable-handle').each(function(i, v){
      $(v).remove();
   });


   /**
    * Iterating through .card again and making it's children draggable and resizable but this does not work.
    **/
   $('.card').children().each(function(i, v){
      if($(v).attr('data-type') == 'input'){
        $(v).draggable({containment: '.card-wrapper'}).resizable({
          handles: "n, e, s, w"
        });
      }
   });
});

After doing this, draggable works but resizable does not. 完成此操作后, draggable作品有效,但resizable不可。

Please help me out to fix this. 请帮我解决此问题。

Thanks in advance. 提前致谢。

Edit: Here's the Fiddle 编辑:这是小提琴

Two things I found odd, 我发现有两件事很奇怪,

  1. You are removing child elements and later trying to add resizable on same. 您要删除子元素,然后尝试在其上添加resizable
  2. You are binding draggable and resizable at the same time. 您同时绑定了draggableresizable It does not work since the return of the draggable is not an instance of the element. 它不起作用,因为draggable的返回不是该元素的实例。

I have tried to fix your issue abe below 我已尝试在下面解决您的问题

 $(function() { /** * Iterating through .card again and making it's children draggable and resizable but this does not work. **/ $('.card').children().each(function(i, v) { if ($(v).attr('data-type') == 'input') { $(v).resizable({ handles: "n, e, s, w" }); $(v).draggable({ containment: '.card-wrapper' }) } }); }); 
 .card { outline: solid 1px #aaa; position: absolute; margin: auto; top: 0; bottom: 0; left: 0; right: 0; } .card .card-text { position: absolute; left: 32%; border: solid 1px red; } 
 <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> <!-- div with the class .card is not dynamic but it's content is which is coming from database and i'm using PHP & MySQL for backend --> <div class="card"> <div data-type="input" data-card="serial_no" data-title="Serial No" class="card-data card-text card-content serial_no serial_no_content ui-draggable ui-draggable-handle ui-resizable"><span>Content for Serial No</span> <div class="ui-resizable-handle ui-resizable-n">foo</div> <div class="ui-resizable-handle ui-resizable-e">too</div> <div class="ui-resizable-handle ui-resizable-s">poo</div> <div class="ui-resizable-handle ui-resizable-w">choo</div> </div> </div> 

Hope this helps. 希望这可以帮助。

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

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