jQuery Droppable - 如果Dropped Area有超过3个<li>元素,则添加CSS类

时间:2015-05-07 11:10:47

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

我正在使用jQuery Droppable插件。

如果我的丢弃区域达到超过3个<li> 元素,我想添加一个CSS类&#34; stackFavs&#34; ,否则相同必须删除课程。

当我尝试使用下面的代码仅针对Drag n Drop功能:时,会发生这种情况。但我也有onClick方法,它在这种情况下无效。

if($('.header-favorites .h-droped-list li').length > 3) {
    $(".h-droped-list").addClass("stackFavs");
}

FIDDLE

  

的jQuery

/* Menu Items Drag n Drop to create Short Cuts in Favorites Bar */
$(document).ready(function(){

    $('.rp-draggable li').not('li.pd-dropdown').each(function (i) {
        $(this).attr('uuid', + i);
    });

    /* jQuery Droppable */
    $(function() {
        $( ".mn-items .rp-draggable li" ).not('li.pd-dropdown').draggable({
            helper: "clone",
            placeholder: "ui-state-highlight",
        });
        $( ".header-favorites ul" ).droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: ":not(.ui-sortable-helper)",
            drop: function(event, ui) {
                $(this).find(".placeholder").hide();
                $(ui.draggable).addClass("addedToFav").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(){
        var $target = $(this).closest("li"),
            $dropedList = $(".h-droped-list"),
            id = $target.attr("uuid");

        if(!$target.hasClass("addedToFav")){
            $target.addClass("addedToFav").clone().appendTo($dropedList);
            $dropedList.find(".placeholder").hide();
        }else{
            $dropedList
            .find("li")
            .each(function(index, item) {
                var $elem = $(item);

                if($elem.attr("uuid") == id){
                    $elem.remove();
                    $target.removeClass("addedToFav"); 
                }

                if($dropedList.children().length == 1){
                    var $lastItem = $($dropedList.children()[0]);
                    $lastItem.hasClass("placeholder") && $lastItem.show();
                }
            })
        }

    });

    /* Click Close Icon to Remove from Drop Here Container */
    $("ul.h-droped-list").on('click','li .fa-star-o',function(){
        var $target = $(this).closest("li"),
            $catalog = $("#catalog ul"),
            id = $target.attr("uuid"),
            $dropList = $target.parent("ul");

        $target.remove();

        $catalog
        .find("li")
        .each(function(index, item){
            var $elem = $(item);

            if($elem.attr("uuid") == id)
                $elem.removeClass("addedToFav");
        })

        if($dropList.children().length == 1){
            var $lastItem = $($dropList.children()[0]);
            $lastItem.hasClass("placeholder") && $lastItem.show();
        }

    });



});
  

HTML

<div class="mn-items">
    <h2>Drag</h2>
    <ul class="rp-draggable">
        <li>Item 1 <i class="fa fa-star-o"></i></li>
        <li>Item 2 <i class="fa fa-star-o"></i></li>
        <li>Item 3 <i class="fa fa-star-o"></i></li>
        <li>Item 4 <i class="fa fa-star-o"></i></li>
        <li>Item 5 <i class="fa fa-star-o"></i></li>
        <li>Item 6 <i class="fa fa-star-o"></i></li>
    </ul>
</div>

<div class="header-favorites">
    <h2>Drop Here...</h2>
    <ul class="h-droped-list">
        <li class="placeholder">Placeholder (hides if it has items)</li>
    </ul>
</div>
  

参考屏幕截图 - 最多3个元素   Reference Screenshot - Till 3 Elements

     

参考屏幕截图 - 超过3个元素   Reference Screenshot - More than 3 Elements

2 个答案:

答案 0 :(得分:1)

将元素追加到droppable div时检查附加的li数

if($('.h-droped-list li').length <= 3){
   // if number less or equal 3 
   // $('.h-droped-list li').removeClass('.class_bigger_than_three').addClass('.class_less_than_three');
}else{
   // if number bigger than 3 
   // $('.h-droped-list li').removeClass('.class_less_than_three').addClass('.class_bigger_than_three');
}

在您的情况下,您可以使用.addClass()removeClass()方法在类之间进行转换

答案 1 :(得分:1)

我做了一些修复,让代码按预期工作。

类似的代码(查看非占位符选择器):

if ($('.header-favorites .h-droped-list li:not(.placeholder)').length > 3) {
    $(".h-droped-list").addClass("stackFavs");
} else {
    $(".h-droped-list").removeClass("stackFavs");
}

被解雇:

  • drop event
  • 星级点击活动
  • 删除点击事件

我已将catalog ID添加到主div或删除不工作并加速删除bt删除每个并使用attr选择器:

$catalog.find("li[uuid='"+id+"']").removeClass("addedToFav");

演示:http://jsfiddle.net/j8xu2tvL/