2013-02-12 87 views
4

我想使用jQuery的ui可拖動和droppable,它工作正常,如果我嘗試在同一頁上。但是,當我嘗試拖動一個元素並放入iframe中定義的可放置區域時,它不能正常工作。它可以工作,但不在iframe的整個可投放區域,也可以在框架之外投放。jQuery的可拖動拖放到一個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> 

回答

相關問題