jQuery UI draggable is not working on newly create

2019-04-23 05:11发布

I have some DOM element which is draggable using jQuery UI.All are working fine but when i create some element using jQuery , then they are not draggable at all. i.e

$('div.draggable').draggable(); //Existing element , it works :)
$('p.draggable').draggable() ; //Newly created paragraph with same class name, it doesnt work at all :(

Thanks in advance !!!

I am trying This :

<script type="text/javascript">
     $(document).ready(function(){
         $('body').append('<p class="draggable">Newly Created Paragraph</p>');
         $('p.draggable').draggable(); **//This is not working**
     });
</script>

However Somehow this is working

    <script type="text/javascript">
     $(document).ready(function(){
         $('body').append('<p class="draggable">Newly Created Paragraph</p>')
                 .find('p.draggable').draggable(); **This is working**

     });
</script>

7条回答
The star\"
2楼-- · 2019-04-23 05:33

You need to call draggable() after the newly created element is inserted into the DOM.

The reason being, is that the first line of code is only matching existing elements. Newly created elements aren't selected in that first line.

查看更多
女痞
3楼-- · 2019-04-23 05:37

Actually it seems that there is problem with draggable. When you append some htmlTag and then try to find it and make it draggable, it do not recognise it. try to make a new element using createElement and then append it. Hope it works

var newEle = document.createElement('p');
$(newEle).attr("class","draggable").draggable();
$('body').append($(newEle));

or

var newEle = document.createElement('p');
$(newEle).attr("class","draggable");
$('body').append($(newEle));
$('p.draggable').draggable();
查看更多
女痞
4楼-- · 2019-04-23 05:38

You can use setTimeout to call draggable function after the new element has made.

$('div').click(function(){
 $('body').append('<div class="box"></div>');
 setTimeout(function() {
  $('div.box').draggable({
   drag: function( event, ui ) {},
   cursor:'-webkit-grabbing'
  });
 },1000);
})
查看更多
家丑人穷心不美
5楼-- · 2019-04-23 05:40

You should call draggable() function everytime you call an event action:

$('body').on('click', '.add-new-table', function () {
 $( ".canvas" ).prepend('<div class="ui-widget-content draggable"><p>Drag me</p></div>');
 $(function(){
   $(".draggable" ).draggable({
     containment: "parent"  
   });
 });
});
查看更多
forever°为你锁心
6楼-- · 2019-04-23 05:46

i had this problem but after reading this i could solve it. so you have to call again draggable() method after building your new element, this is my code:

$(".in-browser").each(function(){
        $(this).dblclick(function(){
            var browseIn = $(this).data("href");
            var browserHTML = '<div class="browser draggable">\
        <ul class="browser-buttons">\
            <li>_   \
            <li>#   \
            <li>x   \
        </ul>\
        <div class="browser-win-container">\
        <div class="addressbar"></div>\
        <iframe class="opening-window" src="'+browseIn+'"></iframe>\
    </div>\
    </div>';
            $("body").append(browserHTML);
            $(".draggable").draggable(); //look at here
        });
    });
查看更多
唯我独甜
7楼-- · 2019-04-23 05:47

You have to apply draggable on the instance of the newly created object, Rewritten code:

<script type="text/javascript">
 $(document).ready(function(){
    var newElement = '<p class="draggable">Newly Created Paragraph</p>';
    $('body').append(newElement);
    newElement.draggable(); **//This is working**
 });

It should work now.

查看更多
登录 后发表回答