2

我们的问题:我在 iframe 外面有一个可拖动的东西,里面有一个可放置的目标。在这里,我将 iframe 显示为包含由其 src 属性加载的 HTML 片段。

所以见代码:

内部 iframe 页面 (inner_iframe.html):

<body style="cursor: auto;">
    <div id="container">
    </div>
</body> 

主页 :

<div id="main">
    <iframe id="containeriframe" src="inner_iframe.html"></iframe>
</div>
    <div id="container1">
        <div class="drag" style="left: 20px;" id="lable"></div>
    </div>

JavaScript 代码:

 $("#containeriframe").load(function () {
          var $this = $(this);
          var contents = $this.contents();
          // here, catch the droppable div and create a droppable widget
          contents.find('#container').droppable({
              iframeFix: true,
              drop: function (event, ui) { alert('dropped'); }
          });
      });


$( "#lable" ).draggable({
        revert: "invalid", 
        helper: "clone",
        cursor: "move",
        iframeFix: true
    });

现在我使用 Jquery 1.8 和 Jquery UI。

所以我加载页面并尝试放入 iframe div 但没有响应,那么如何管理它。

请帮我 ....

谢谢

4

2 回答 2

4

这对我有用:

主页:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js""></script>
     <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
    <script>
        $(function () {

            $("iframe").load(function () {
                var iframe = $(this).contents();
                iframe.find('#iframe_container').droppable(
                {
                    drop: function (event, ui) { alert('dropped'); }
                });
            });

            $('#drag').draggable();


        });
    </script>
</head>
<body>

    <iframe src="iframe.html"></iframe>

    <div  style="width:20px; height:20px; background-color: #808080" id="drag"></div>

</body>
</html>

框架:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>

    <div id="iframe_container" style=" width: 40px; height: 40px; background-color: #0094ff">
    </div>


</body>
</html>
于 2012-08-29T05:18:30.307 回答
1

试试这个插件http://maxazan.github.io/jquery-ui-droppable-iframe/

<!--jquery -->
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<!--jquery UI -->
<script type="text/javascript" src="js/jquery-ui-1.11.4.custom.js"></script>
<!-- jquery-ui-droppable-iframe -->
<script type="text/javascript" src="jquery-ui-droppable-iframe.js"></script>    
<!--Activate drag and drop zones -->
<script type="text/javascript">
$(function() {
    //After frame loaded
    $("#testframe").load(function() {
        //Activate droppable zones
        $(this).contents().find('.droppable').droppable({
            drop: function(event, ui) {
                //ACTION ON DROP HERE
            }
        });
    });

    //Activate draggable zones
    $('.draggable').draggable({
        iframeFix: true,    //Core jquery ui params needs for fix iframe bug
        iframeScroll: true  //This param needs for activate iframeScroll
    });

});
</script>
于 2015-11-14T11:29:38.047 回答