【问题标题】:Drag an item from parent frame and drop it in a droppable object in child iframe从父框架中拖动一个项目并将其放置在子 iframe 中的可放置对象中
【发布时间】:2013-03-26 20:07:19
【问题描述】:

我正在尝试将项目从父框架拖到可放置元素中,但在 iframe 中。它在将项目放在同一帧中的可放置元素上时有效,但在跨帧尝试时,它什么也不做。

我已经尝试了很多次,但都没有成功。

这是父框架的代码:

<html>
<head>
    <title></title>
    <link rel="stylesheet"                             href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<style>
  #draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  #droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
  </style>
  <script>
  $(function() {
    $( "#draggable" ).draggable();
    $( "#droppable" ).droppable({
      drop: function( event, ui ) {
        $( this )
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
      }
    });
  });
  </script>
</head>
<body>
    <div id="draggable" class="ui-widget-content">
  <p>Drag me to my target</p>
</div>

<div id="droppable" class="ui-widget-header">
  <p>Drop here</p>
</div>
<iframe src="P1.html" width = "500px" height = "500px">
</iframe>

</body>
</html>

这是 iframe 的代码:

<html>
<head>
    <title></title>
    <link rel="stylesheet"                             href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<style>
  #draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  #droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
  </style>
  <script>
  $(function() {
    $( "#draggable" ).draggable();
    $( "#droppable" ).droppable({
      drop: function( event, ui ) {
        $( this )
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
      }
    });
  });
  </script>
</head>
<body>
    <div id="draggable" class="ui-widget-content">
  <p>Drag me to my target</p>
</div>

<div id="droppable" class="ui-widget-header">
  <p>Drop here</p>
</div>    
</body>
</html>

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-ui-draggable jquery-ui-droppable


    【解决方案1】:

    不幸的是,框架是独立的窗口对象,因此您无法将对象从一个 DOM 树拖到另一个窗口的 DOM 树中。网页基于窗口 DOM 的当前状态呈现。当您拖动元素时,DOM 会发出重新绘制的信号,并且网页会根据元素的样式重新呈现(在拖动的情况下,顶部和左侧的样式属性通常是罪魁祸首)。

    当您的页面上有两个框架(或窗口对象)时(在这种情况下,一个在另一个内部),您也有这个 DOM 对象的两个完全独立的实例,因此,它的行为。当一个在另一个内部并一起执行一个更大的 DOM 渲染时,DOM 不会协作;它们是完全不同的范围(包括 JavaScript),它的设计方式是有原因的(主要是为了安全,因为您可以将外部网站加载到嵌入式窗口/iFrame 对象中)。

    【讨论】:

    • 这确实不能提供答案(例如匹配其他 iframe 元素的位置),并且每个 iframe 将的 JS 都可以相互访问它们是同一个域。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2010-10-26
    相关资源
    最近更新 更多