4

我正在尝试使用 jQuery ui 可拖动和可放置,如果我在同一页面上尝试,它工作正常。但是当我尝试将一个元素拖放到 iframe 中定义的可放置区域时,它就不能完美地工作。它有效,但不适用于 iframe 中的整个可放置区域,并且它也可放置在框架外。

这是我所做的

我的主页:Home.html

<!DOCTYPE html>
<html><head>
    <title>jQuery UI Draggable</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
    <link href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" rel="stylesheet">

    <script type="text/javascript">
        $(function () {
            $("#FirstDiv").draggable({ revert: "invalid",iframeFix: true });
            $("#SecondDiv").draggable({ revert: "invalid" });
            $("#frame").load(function () {
                var $this = $(this);
                var contents = $this.contents();
                contents.find('.DroppableDiv').droppable({
                    drop: function (event, ui) { alert('dropped'); }
                });
            });
        });
    </script>
</head>
<body style="cursor: auto;" >

<div class="demo">
<div id="mainDiv" style="background-color: #FAE6B6; height:200px">
<div id="FirstDiv" style=" border-style:solid; border-width:1px; font-size:medium; height:20px; width:300px">First Div</div>
<div id="SecondDiv" style=" border-style:solid; border-width:1px; font-size:medium; height:20px; width:300px">Second Div</div>
</div>
</div>
<iframe id="frame" src="test.html" style="width: 800px; height: 400px"></iframe>

</body></html> 

这是我的 iframe 内容 test.html

<!DOCTYPE html>
<html>
<head>
    <title>jQuery UI Draggable - inner html</title>
    <style>
     .DroppableDiv {
        background-color:red;
        height: 500px;
        width: 500px;
     }
    </style>
</head>
<body style="cursor: auto;">
    <div class="DroppableDiv">
    </div> 
</body>
</html>
4

1 回答 1

0

您可以尝试以下方法:

$('#draggables div', parent).draggable();

这里也有几个不同的答案:

https://web.archive.org/web/20170426075018/http://jqfaq.com/how-to-implement-drag-and-drop-between-iframes/

jQuery 和 iframe 以及奇怪的定位:有解决方法吗?

祝你好运!

于 2014-02-23T16:52:08.280 回答