【问题标题】:jQuery Droppable - Not able to remove Cloned ElementjQuery Droppable - 无法删除克隆元素
【发布时间】:2015-04-24 11:17:50
【问题描述】:

我正在使用 jQuery UI Droppable...

我需要像拖放一样的相同功能来单击 Draggable 中的星形图标,它必须添加到 Droppable 容器中。我试过了。

但是,当我试图从 Droppable 容器中删除 <li> 元素时,相同的技巧(克隆)不起作用......这里..点击关闭图标它应该只关闭相关的 <li> 从可拖动区域。

有什么快速的建议吗?

是否可以:

  • 克隆(单击星形图标)限制为仅一次...同时为多次单击创建多个 <li>

FIDDLE


截图供参考


HTML

<div class="mn-items">
  <h2>Drag</h2>
  <div id="catalog">
      <ul class="rp-draggable">
        <li class="one">Item 1 <i class="fa fa-star-o"></i></li>
        <li class="two">Item 2 <i class="fa fa-star-o"></i></li>
        <li class="three">Item 3 <i class="fa fa-star-o"></i></li>
        <li class="four">Item 4 <i class="fa fa-star-o"></i></li>
        <li class="five">Item 5 <i class="fa fa-star-o"></i></li>
        <li class="six">Item 6 <i class="fa fa-star-o"></i></li>
      </ul>
  </div>
</div>

<div class="header-favorites">
  <h2>Drop Here...</h2>
    <ul class="h-droped-list">
      <li class="placeholder">Add your items here</li>
    </ul>
</div>

jQuery

$(document).ready(function(){

    /* jQuery Droppable */
    $(function() {
        $( ".mn-items .rp-draggable li" ).draggable({
            appendTo: "body",
            helper: "clone"
        });
        $( ".header-favorites ul" ).droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: ":not(.ui-sortable-helper)",
            drop: function( event, ui ) {
                $( this ).find( ".placeholder" ).remove();
                $(ui.draggable).clone().appendTo(this);
            }
        }).sortable({
            items: "li:not(.placeholder)",
            sort: function() {
                $( this ).removeClass( "ui-state-default" );
            }
        });
    });


    /* Click Star Icon to Add to Drop Here Container */
    $('ul.rp-draggable li .fa-star-o').click(function(){
        $(this).closest('li').clone().appendTo('.h-droped-list');
        $(this).closest('li').toggleClass('addedToFav');
    });

    /* Click Close Icon to Remove from Drop Here Container */
    $("ul.h-droped-list li .fa-star-o").click(function(){
        $(this).closest('li').remove();
    });

});

【问题讨论】:

    标签: jquery jquery-ui jquery-draggable jquery-droppable


    【解决方案1】:

    删除按钮lis 是动态添加的,您需要使用事件委托将事件附加到它们:

    $("ul.h-droped-list").on('click','li .fa-star-o',function(){
        $(this).closest('li').remove();
    });
    

    Working Demo

    【讨论】:

    • 感谢您的快速回复...。它的工作方式类似于 CHAMP.. 但它并没有从 Drag Container 中取出 CSS 类 addedToFav.. .
    • 您需要维护一些属性或类,当相应的新元素被删除时,这些属性或类将用于定位拖动容器元素。
    • 试过但失败了 :( .... 我可能有 100 个菜单项。在这种情况下,我该如何处理?请帮我解决 Fiddle ...因为我不擅长 jQuery 接收复制粘贴 :)
    • 克隆时首先关联一些东西(如类或属性)。
    【解决方案2】:

    问题是您需要将点击事件委托给其父级以动态添加元素

     /* Click Close Icon to Remove from Drop Here Container */
    $('.header-favorites').on('click',"ul.h-droped-list li .fa-star-o",function(){
       $(this).closest('li').remove();
    });
    

    demo

    【讨论】:

    • 但是如何从 Drag 容器中删除相关的 &lt;li class='addedToFav'&gt; 类?
    猜你喜欢
    • 2016-04-08
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多