jQuery-ui 可在 iframe 内放置到可排序

jQuery-ui droppable to sortable inside iframe

我在主框架中有 droppable 个元素,在 iframe 应用程序中有 sortable 个元素。我需要的是连接它们——能够将项目拖到 iframe 的 sortable

这是我所做的: http://jsfiddle.net/w9L3eorx/1/

里面iframe我有

<div class="content">
    <div class="block">Foo</div>
    <div class="block">Bar</div>
</div>

<script>
    $('.content').sortable({
        iframeFix: true,
        placeholder: 'block-placeholder',
        update: function (event, ui) {
            // turn the dragged item into a "block"
            ui.item.addClass('block');
        }
    });
</script>

主机

<div class='items'>
    <div class="one">One</div>
    <div class="two">Two</div>
</div>

<iframe src="frame.html" id="frame" width="800" height="800"></iframe>

<script>
    $('.items div').draggable({
        helper: function(e) {
            return $('<div>').addClass('block').text( $(e.target).text() );
        },
        iframeFix: true,
        connectToSortable: $('.content', $("#frame")[0].contentDocument)
    });
</script>

我看到了工作示例 http://ma.rkusa.st/zepto-dnd/example.html。但它是在没有 jquery 的情况下构建的,并且不适用于 IE9

给你:

    $('.items div').draggable({
        helper: function(e) {
            return $('<div>').addClass('block').text( $(e.target).text() );
        },
        iframeFix: true,
        connectToSortable:$('#frame').contents().find('.content').sortable({
            iframeFix: true,
            placeholder: 'block-placeholder',
            update: function (event, ui) {
                // turn the dragged item into a "block"
                ui.item.addClass('block');
            }
        })
    });

FIDDLE: http://jsfiddle.net/w9L3eorx/5/

请注意,您的 iframe 应该只是普通的 HTML(不要在那里初始化 sortable 否则它会出现异常)

我已经更改了 boszlo 示例以满足我的需要:

  • 我需要先在 iframe 中初始化 sortable(我的可放置侧边栏将在点击后出现)
  • 我需要在父(主)框架中重新初始化 sortable 并连接 dropable 但 完全相同的选项.

http://jsfiddle.net/w9L3eorx/8/

所以在 iframe 我添加了函数

window.destroySortableAndGetOptions = function (selector) {
    var opts = $(selector).sortable('option');
    $(selector).sortable('destroy');
    return opts;
}

这将破坏 sortable 和 returns 选项。

并且在 主框架 之前 droppable 初始化,我销毁 sortable 并选择选项

var sortableOptions = document.getElementById('frame').contentWindow.destroySortableAndGetOptions('.content');

并使用相同的选项重新初始化 sortable

...
connectToSortable:$('#frame').contents().find('.content').sortable(sortableOptions)
...