首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery-ui可拖动和动态Jquery-ui可拖动?

Jquery-ui可拖动和动态Jquery-ui可拖动?
EN

Stack Overflow用户
提问于 2011-10-05 19:31:27
回答 4查看 10.3K关注 0票数 9

这是我的代码摘自http://jqueryui.com/demos/draggable/

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>jQuery UI Draggable - Default functionality</title>
  <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
  <script src="../../jquery-1.6.2.js"></script>
  <script src="../../ui/jquery.ui.core.js"></script>
  <script src="../../ui/jquery.ui.widget.js"></script>
  <script src="../../ui/jquery.ui.mouse.js"></script>
  <script src="../../ui/jquery.ui.draggable.js"></script>
  <link rel="stylesheet" href="../demos.css">
  <style>
  .draggable { width: 150px; height: 150px; padding: 0.5em; }
  </style>
  <script>
   $(function() {
    $( ".draggable" ).draggable();
    $('.content').click(function(){
     var htmlData='<div  class="draggable ui-widget-content      ui-draggable"><p>Drag me around</p></div>';
     $('.demo').append(htmlData);
    });
   });
  </script>
 </head>
 <body>
  <div class="demo">
   <div class="draggable ui-widget-content">
    <p>Drag me around</p>
   </div>
   <div class="content">Test </div>
  </div><!-- End demo -->
 </body>
</html>

正如您在函数Iam中使用append所看到的,iam正在动态地使组件可拖动。但是新生成的drggable对象不能拖动,尽管我已经给出了jquery-ui生成的类。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-10-05 19:37:48

添加动态创建的元素后,尝试调用$( ".draggable" ).draggable();

代码语言:javascript
复制
$(function() {
    $( ".draggable" ).draggable();
    $('.content').click(function(){
         var htmlData='<div class="draggable ui-widget-content ui-draggable"><p>Drag me around</p></div>';
         $('.demo').append(htmlData);
         $( ".draggable" ).draggable();
    });
});
票数 16
EN

Stack Overflow用户

发布于 2012-01-18 09:11:28

实际上,出于性能原因,我将使用:

代码语言:javascript
复制
$('.draggable:not(.ui-draggable)').draggable();

这将防止尝试使用jQuery UI的内置类重新初始化已经可拖动的元素。此外,如果您的代码修改了单个可拖动对象的属性,而该属性可能与新可拖动对象的属性不匹配,则最终可能会重置一些可拖动对象。

具体使用jQuery并没有什么错。

更新

没有指定哪个实例使用它。基于Ricardo的编辑:

代码语言:javascript
复制
$(function() {
    $( ".draggable" ).draggable();
    $('.content').click(function(){
        var htmlData='<div  class="draggable ui-widget-content"><p>Drag me around</p></div>';
        $('.demo').append(htmlData);
        $('.draggable:not(.ui-draggable)').draggable(); //Here
        });
   });

我现在还看到您手动添加了ui-draggable类。你不需要这样做。事实上,它使我所说的不起作用。

票数 4
EN

Stack Overflow用户

发布于 2012-01-16 01:49:32

代码语言:javascript
复制
<script>
    $(function() {
        $(".draggable").draggable();
        $(".content").click(function(){
            var htmlData='<div class="draggable ui-widget-content ui-draggable"><p>Drag me around</p></div>';
            $(".demo").append(htmlData);
        });
    });
</script>

只需要改变位置就行了

代码语言:javascript
复制
<script>
    $(function() {    
        $(".content").click(function(){
            var htmlData='<div class="draggable ui-widget-content ui-draggable"><p>Drag me around</p></div>';
            $(".demo").append(htmlData);
            $(".draggable").draggable(); //bring it here so that for the newly inserted/created dom elements, jquery draggable can be initialized.
        });
    });
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7660606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档