如果已经删除了div,则使用jquery

时间:2016-02-18 06:09:08

标签: javascript jquery

下面是我的拖放脚本,它看起来像是一个魅力,但我要添加的是,如果我在已经包含相同div的div上放置一个可拖动的div,它应该给我一个警告这个可拖动的元素已经存在或类似的东西,为了实现这一点,我应用了.prevAll.each()函数来获取id并与最后删除的div进行比较。所以它比较了id很好,但是当我删除已经存在的div时,它完全跳过.prevAll.each()条件,它会像其他人一样警告自己的id,但它不会进入.prevAll,为什么会这样?

总结我想要的是:

  1. 如果已经存在div,则编辑下面的方法以获取警报。
  2. 或者任何其他方法也会受到赞赏,如果它给了我相同的功能并且不影响任何其他的东西,我已经尝试了.length方法它不起作用。
  3. 我希望我能够解释,任何帮助将不胜感激,谢谢

    $(".dragable").draggable({
        cancel: "a.ui-icon",
        revert: true,
        helper: "clone",
        cursor: "move",
        live: true,
        revertDuration: 0
    
    
        });
    
    $('.droppable').droppable({
        accept: ".dragable",
        activeClass: "ui-state-highlight",
        drop: function(event, ui) {
        var $item = $(ui.draggable);
        if (!$item.hasClass('clone')) {
        $item = $item.clone().addClass('clone');
        $item.draggable({
            cancel: "a.ui-icon",
            revert: true,
            cursor: "move",
            revertDuration: 0
            });
    }
    $(this).addClass('has-drop').append($item);
    var divIdIs = $(ui.draggable).attr( "id" );
    //if($(".droppable:has()"))
    /*if($("#"+divIdIs).length>1){
    alert("Yeah it does exist");
    }*/
    $("#"+divIdIs).prevAll().each(function() {
        var upperDiv = $(this).attr('id');
        /*var tes=$("#"+divIdIs).length;
        alert(tes);*/
        if(divIdIs == upperDiv){
        alert("Matched");
        }
        else{
        alert("Not Matched");
        }
    
        //if(divIdIs == existingdivId){}
        //else{}
    
        });
    

1 个答案:

答案 0 :(得分:2)

drop: function(event, ui) {

替换上面的行,然后尝试

if (ids.indexOf("," + ui.draggable[0].id + ",") >= 0)
{
   alert("This div already exists");
   return;
}
ids += ui.draggable[0].id + ",";

还要确保全局声明以下变量

var ids = ",";
相关问题