【问题标题】:jQuery droppable $(this) returns wrong ID with overflow:autojQuery droppable $(this) 返回错误的 ID 并溢出:auto
【发布时间】:2011-03-26 22:21:57
【问题描述】:

我在一个页面上有许多 div 溢出:自动,许多 li 的数量超出了可见区域的容量。当我从一个 div 拖放到另一个 div 时,如果目标 div 上方的 div 有一个长列表,则 $(this) 将返回该 div 的 id,而不是我的项目所在的 div。在视觉上和在修改后的 html 中它工作正常,但我需要能够获得它所在的 div 的 id 并且似乎无法弄清楚。似乎必须有一个解决方法???

我的目标是可靠地获取接收 div 的 id。到 drop 事件触发时,该位置尚未更改。也许它可以通过 id 以相反的顺序初始化每个可放置的 div,而不是使用“可排序”类,但可以有 10 或 15 个,而且看起来非常笨拙。

这是我的初始化代码(在尝试 greedy:true 之后):

$( ".sortable" ).sortable({
        connectWith : ['.sortable'],
        dropOnEmpty : true,
        placeholder : "ui-state-highlight",
        revert: true,
        start  : function(event, ui) {
            getEquip($(ui.item).attr('id').slice(8));
        }
    });
    $( ".sortable" ).draggable({
        helper: "clone",
        revert: "invalid"
    });
    $( ".sortable" ).droppable({
        greedy : true,
        drop: function( event, ui ) {
            var msg = 'Moved ' + $(ui.draggable).text() + ', id#' + $(ui.draggable).attr('id').slice(8) + ' to';
            msg += ' ' + $(this).attr('id'); //<<== gives wrong id if list above is long 
            msg += ' ' + $('#' + $(ui.draggable).attr('id')).parent().attr('id');
            writeFooter(msg);//Moved hydrant wrenches, id#164 to sortable16 sortable22  
        },
    });

【问题讨论】:

  • 您是否将选项 greedy: true 添加到您的可放置对象中?请提供一些代码,以便我们为您提供帮助
  • 感谢您的回复!我尝试贪婪没有成功,并在上面添加了一些代码。

标签: jquery overflow droppable


【解决方案1】:

找到了解决方法。如果我按 id 从下到上初始化可放置的 div,即以相反的顺序,问题就消失了。以前我是按类初始化它们。修改后的代码 sn-p 如下所示,其中 subloc[i-1][0] 是子位置 id#:

for (var i = subloc.length; i > 0; i--) { //init droppables backwards to overcome overflow bug
        $( "#sortable" + subloc[i-1][0]).droppable({
            tolerance : 'intersect',
            drop: function( event, ui ) {...

【讨论】:

    猜你喜欢
    • 2014-10-02
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2022-12-08
    相关资源
    最近更新 更多