【问题标题】:Load Clicked Id's Website in iframe on Click for Website in Database在点击数据库中的网站时在 iframe 中加载 Clicked Id 的网站
【发布时间】:2016-09-22 11:56:27
【问题描述】:

当前网站: enter image description here

我有一个网站,它是通过使用 php 和 mysql 调用我的数据库的 for 循环生成的。我有身份证、姓名、公司网站、地址和图片。它目前显示名称、描述和图像,所有这些都显示在堆叠的方形框中。

单击该框时,我正在尝试加载公司网站的 iframe,然后覆盖该框。当我加载网站时,我已经能够加载所有网站 iframe,但如果有超过 5-6 行数据,则网站加载速度非常慢,因为它正在加载大约 7 个网站。

我大部分时间都在查看 jquery 和 iframe 问题,但仍然无法让我的 iframe 在您单击该框时单独加载。我的意图是让用户点击描述/公司框而不是 iframe。然后,这将加载 iframe 并将其显示在公司框上,并在其中填充/隐藏框中的内容,完全覆盖框内容。

如何加载在 iframe 中打开的网站 onclick 而不加载动态生成的页面内的所有 iframe。

    <?php require_once('include/header.php')?>
    <? require('include/navigation.php')    ?>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Coworking Spaces</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/mystyles'); ?>">

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
    <script>
    jQuery(function(){
        $("iframe").each(function(){
            this.tmp = this.src;
            this.src = "";
        })
        .parent(".website")
        .click(function(){
            var frame = $(this).children("iframe")[0];
            console.log(frame);        frame.src = frame.tmp;
        });
    });
        </script>
    </script>
    </script>
    </head>
    <body>

    <div id="container">
        <div id='city_header'>
            <div id='city_search'>
                <h1>
                    Kansas City
                </h1>
            </div>
        </div>
        <div id="body">
                    <?php for($i = 0; $i < count($cospaces); ++$i){?>
                    <div class="company_box" style ="background-image: url(<?php echo($cospaces[$i]['image'])?>);">

                    <div class="name_box">
                        <h3 class="space_name"><?php echo($cospaces[$i]['name'])?></h3>
                    </div>

                    <div class="description">
                        <p><?php echo($cospaces[$i]['description']) ?></p>
                    </div>
                    <!-- iframe wanting to load -->
                    <iframe id = "website" data-src="<?php echo($cospaces[$i]['website'])?>" width="200" height="200" style="background:#ffffff"></iframe>

            </div>
            <?php } ?>
        </div>
        <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
    </div>
    </div>
    </body>
    </html>

已测试建议的实施:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(function($) {
        $('iframe').bind('load', function() {
           var childFrame = $(this).contents().find('iframe');
           var childElement = childFrame.contents().find('body');
           var src =  $(this).attr('data-src');
           childElement.on('click', function() {
              childFrame.attr('src', src);
           });
        });
    });
    </script>

  </head>
  <body>
    <iframe data-src="https://jsfiddle.net" srcdoc="<iframe id='hi' srcdoc='<body>Click me</body>'></iframe>">
    </iframe>
  </body>
</html>

【问题讨论】:

  • 真正的问题是什么?
  • 如何加载在 iframe 中打开的网站 onclick 而不加载动态生成的页面内的所有 iframe。 (我也用这个更新了问题。)

标签: javascript php jquery html iframe


【解决方案1】:

我没有看到 this.src 在 PHP 生成的 iframe 中为您的初始 .each 函数调用定义的位置。

我也不确定您为什么要调用.parent('.website'),因为这会向上遍历以查找带有class="website" 的包装元素,其中PHP 生成的iframe 除了带有id="body" 的div 之外没有父元素。 例如,您的代码正在寻找:

<div class="website">
    <iframe></iframe>
</div>

使用 iFrame 时要记住的一点是,它们的内容在 DOMReady 上不可用,因此您必须将负载侦听器附加到要修改其内容的每个 iFrame。

如果我理解正确,您只是希望 iframe 的 src 在用户单击 iframe 时加载。你有没有尝试过类似的东西

jQuery(function($) {
   $('iframe').on('click', function(e) {
      var frame = $(this);
      frame.attr('src', frame.attr('data-src'));
      frame.off('click'); //remove on-click event to prevent reloading on click
   });
});

iFrame 目标很小,只会在点击 iFrame 的外边框而不是其内容体时触发点击事件。 iFrame 在 DOM 事件方面的行为不像 &lt;div&gt; 元素或其他块元素,并且里程会因您可以在每个浏览器和每个版本中执行的操作而有所不同。

除非您尝试在 Coworking Spaces 创建的 iframe 中查找 iframe 并更改其来源,否则使用 Javascript 是不可能的,除非子 iframe 位于同一域中。见:Same Origin Policy

否则你可以做类似的事情

jQuery(function($) {
    $('iframe').bind('load', function() {
       var childFrame = $(this).contents().find('iframe');
       var childElement = childFrame.contents().find('body');
       var src =  $(this).attr('data-src');
       childElement.on('click', function() {
          childFrame.attr('src', src);
       });
    });
});

这将告诉您的脚本等待每个主 iframe 加载,找到其子 iframe,将 onclick 事件附加到子 iframe 主体以将源设置为主 iframe data-src

示例:https://jsfiddle.net/Laomfx7g/

我使用srcdoc 属性将静态 HTML 加载到 iFrame 中以演示绑定到同源内容,因为我无法从 jsfiddle 编辑外部网站的源内容。

最后的元素 id(特别是 for 循环中的 &lt;iframe id="website")应该是唯一的,并且在不同的浏览器中的功能会大不相同。有些只会影响具有指定 id 的第一个元素,有些会影响最后一个元素,其中一些会读取所有指定的 id,这可能是也可能不是有意的。

编辑 - 来自评论澄清

jQuery(function($) {
    $('.description').on('click', function() {
       var frame = $(this).next('iframe');
       frame.attr('src', frame.attr('data-src'));
       $(this).off('click'); //remove click event
    });
});

例如:https://jsfiddle.net/2x0yfpts/

【讨论】:

  • 感谢您的详细回复。它帮助我更好地理解了 DOM。 parent 的原因是这基本上是我对大约 20 个不同的堆栈溢出帖子的最后尝试。当我尝试实施您的解决方案时,我无法让它发挥作用。我什至只用你的代码创建了一个全新的测试文件,但它仍然不起作用。
  • 另外澄清一下,我的意图是让用户点击描述/公司框而不是 iframe。然后,这将加载 iframe 并将其显示在公司框上,在该框内填充该框,完全覆盖网站示例中的框内容。
  • 描述/公司框位于何处。我想你的意思是点击不在 iframe 内的&lt;div id="body"&gt;&lt;div class="description"&gt;?如果是这种情况,则应使用选项 1,将 .on('click') 绑定到描述。我会更新我的答案
  • 公司框是背景图片div,里面有描述、名称和网站iframe。
  • @JustinChristian 更新了答案。这应该让您了解如何使用 jQuery 选择要单击的所需元素并选择任何前面的元素。请务必查看使用 nextnextAllprevprevAllfindparentparents 的 Dom Traversing 的 jQuery 参考(是的,它向上分解直到找到选择器)等api.jquery.com/category/traversing
猜你喜欢
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 2013-11-19
  • 2016-10-30
相关资源
最近更新 更多