使用 jquery 可拖动到可排序的内部

发布于 2024-12-08 11:06:17 字数 2829 浏览 0 评论 0原文

我有一个使用 jquery UI 的可排序的 div 列表。效果很好。

但是,我希望 div 内的某些图像可以拖动。尽管每次我尝试拖动图像时,div 仍然占据优先地位。可排序的 div 会被拖动。

好的,所以我想我需要捕获图像上的 mouseenter 和 mouseleave 事件,并在其悬停时暂时禁用可排序的 div。那不起作用(也许我没有以正确的方式做到这一点)

有人知道如何让它发挥作用吗?

抱歉,如果之前有人问过这个问题,但我搜索时没有找到任何内容。

编辑:花了一些时间才找到确切的位置。无论如何,这是当前的可排序代码。

var oldColumn = '';
var systemColumnCounter = $(".systemColumn").length;
$(".systemColumn").sortable({
    /*
    accept-option mit neuer Version von JqueryUI
    */
    accept: '.none',
    placeholder: 'systemElementPlaceHolder',
    connectWith: '.systemColumn',
    dropOnEmpty: true,
    items: 'div.systemElement',
    forceHelperSize: true,
    start: function(event, ui){
        oldColumn = ui.item.parent();
        $("body").myPopup("hideAll");



    },
    sort: function(event, ui){
        $(this).ElementBorderRemove();
        $(".systemElementPlaceHolder").addClass("systemBorderColor");
        $(".systemElementPlaceHolder").height(ui.helper.height());
    },
    stop: function(event, ui){
        var counter = 0;
        var elementBefor = '';
        var elementAfter = '';
        var Item = ui.item;
        var myColumn = Item.parent();

        if(oldColumn.children().length == 0)
        {
            oldColumn.append('<span class="columnEmptyMessage">Column is empty.</span>');
        }

        myColumn.children().each(function(){    
            if($(this).hasClass("columnEmptyMessage") && myColumn.children().length > 1)
            {
                $(this).remove();
            }               

            if($(this).attr("id") == ui.item.attr("id"))
            {
                if(counter > 0)
                {
                    elementBefor = myColumn.children().eq(counter-1).attr("id");
                }

                if(counter < (myColumn.children().length - 1))
                {
                    elementAfter = myColumn.children().eq(counter+1).attr("id");
                }
                return false;
            }
            counter++;
        })

        serialStr = elementBefor + '|' + elementAfter + '|' + myColumn.attr("id") + '|' + ui.item.attr("id");
        if(!ui.item.hasClass("systemNewElement"))
        {
            $.ajax({
                url: "includes/administration.action.php",
                type: "POST",
                data: "action=elementItemSort&items="+serialStr,
                dataType: "html",
                cache: false,
                success: function(data){                    
                    //alert(data);
                },
                error:function(x,e){
                },
                complete: function(data){
                }
            }); 
        }
    }
});

html 有几个带有 systemColumn 类的 div,里面的东西是可排序的......这部分有效。

I have a sortable list of divs using the jquery UI. It works fine.

However, I would like to have certain images inside the divs be draggable. The divs keep taking priority though every time I try to drag an image. The sortable div gets dragged instead.

Ok, so I figured I need to catch the mouseenter and mouseleave events on the image and temporarily disable the sortable divs while it was hovering. That didn't work (Maybe I didn't do it the right way though)

Does anyone know how to get this to work?

Sorry if this has been asked before, but I didn't find anything when I searched.

EDIT: Took a bit of time to find the exact place. Anyway here is the current sortable code.

var oldColumn = '';
var systemColumnCounter = $(".systemColumn").length;
$(".systemColumn").sortable({
    /*
    accept-option mit neuer Version von JqueryUI
    */
    accept: '.none',
    placeholder: 'systemElementPlaceHolder',
    connectWith: '.systemColumn',
    dropOnEmpty: true,
    items: 'div.systemElement',
    forceHelperSize: true,
    start: function(event, ui){
        oldColumn = ui.item.parent();
        $("body").myPopup("hideAll");



    },
    sort: function(event, ui){
        $(this).ElementBorderRemove();
        $(".systemElementPlaceHolder").addClass("systemBorderColor");
        $(".systemElementPlaceHolder").height(ui.helper.height());
    },
    stop: function(event, ui){
        var counter = 0;
        var elementBefor = '';
        var elementAfter = '';
        var Item = ui.item;
        var myColumn = Item.parent();

        if(oldColumn.children().length == 0)
        {
            oldColumn.append('<span class="columnEmptyMessage">Column is empty.</span>');
        }

        myColumn.children().each(function(){    
            if($(this).hasClass("columnEmptyMessage") && myColumn.children().length > 1)
            {
                $(this).remove();
            }               

            if($(this).attr("id") == ui.item.attr("id"))
            {
                if(counter > 0)
                {
                    elementBefor = myColumn.children().eq(counter-1).attr("id");
                }

                if(counter < (myColumn.children().length - 1))
                {
                    elementAfter = myColumn.children().eq(counter+1).attr("id");
                }
                return false;
            }
            counter++;
        })

        serialStr = elementBefor + '|' + elementAfter + '|' + myColumn.attr("id") + '|' + ui.item.attr("id");
        if(!ui.item.hasClass("systemNewElement"))
        {
            $.ajax({
                url: "includes/administration.action.php",
                type: "POST",
                data: "action=elementItemSort&items="+serialStr,
                dataType: "html",
                cache: false,
                success: function(data){                    
                    //alert(data);
                },
                error:function(x,e){
                },
                complete: function(data){
                }
            }); 
        }
    }
});

The html has a couple of divs with class systemColumn and the stuff inside is sortable... This part works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2024-12-15 11:06:17

您可能只想考虑将 div 的一部分(不包含 img 的部分)作为该 div 上唯一可以使用“handle”选项单击开始排序的位置 http://docs.jquery.com/UI/API/1.8/Draggable#option-handle

您可以让 div 的左侧有某种“移动”光标图像,以便用户看到它并知道单击那里以移动 div 对其进行排序。这消除了代码的复杂性,也消除了用户端的混乱。

You might want to consider only having part of your div (part that the img is not contained in) as the only place on that div you can click to start the sorting by using the "handle" option http://docs.jquery.com/UI/API/1.8/Draggable#option-handle.

You could have the left hand side of the div have some kind of "move" cursor image so the user sees it and knows to click there in order to move the div to sort it. This removes complexity in your code and also removes confusion on the users end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文