jQuery ui draggable放入iframe

时间:2013-02-12 06:21:09

标签: jquery iframe drag-and-drop draggable droppable

我正在尝试使用jQuery ui draggable和droppable,如果我尝试在同一页面上它工作正常。但是当我尝试拖动一个元素并放入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>

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方式:

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

这里还有一些不同的答案:

http://jqfaq.com/how-to-implement-drag-and-drop-between-iframes/

jQuery and iframes and weird positioning: is there a workaround?

祝你好运!