【问题标题】:How to Load iframe content in Popup div?如何在弹出 div 中加载 iframe 内容?
【发布时间】:2014-11-05 01:25:11
【问题描述】:

如何在 Popup div 中加载 iframe 内容。 弹出 div 将在点击每个链接时打开 从每个链接,页面 url 将加载到弹出 div 内的 iframe href。

$(document).ready(function(){
  $(".openpop").click(function(){
    var x =    $(this).attr('href');
    alert(x);
    y =  x.replace('#', '');
    alert(y);
    $(".popup").toggle();
    $("iframe").attr("href", y);

  });

   $(".close").click(function(){
     $(this).parent().fadeOut("slow");
  }); 

});

HTML

<a class="openpop" href="#http://www.google.com">Link 1</a>
<a class="openpop" href="#http://www.yahoo.com">Link 2</a>
<a class="openpop" href="#http://www.w3schools.com">Link 3</a>
<div class="wrapper">
<div class="popup">
<iframe src="">
  <p>Your browser does not support iframes.</p>
</iframe>
<a href="#" class="close">X</a></div>
</div>

【问题讨论】:

  • 最好能得到一个 jsfiddle 以便更好地理解
  • iframe上没有href,应该是src属性,$("iframe").attr("src", y);然后 x-frame-options 会打扰它。

标签: jquery html


【解决方案1】:

首先,链接中不需要#。只需调用e.preventDefault(); 即可停止执行链接。

出于安全原因,您不能打开每个链接,例如。谷歌。

此外,您可能不使用切换,因为如果框架已经打开,您总是必须单击两次。

这是一个working fiddle

html

<div class="links">
    <a class="openpop" href="http://getbootstrap.com/">Link 1</a>
    <a class="openpop" href="http://www.jsfiddle.net">Link 2</a>
    <a class="openpop" href="http://www.w3schools.com">Link 3</a>
</div>
<div class="wrapper">
    <div class="popup">
        <iframe src="">
            <p>Your browser does not support iframes.</p>
        </iframe>
<a href="#" class="close">X</a>
    </div>
</div>

jquery

$(document).ready(function () {
    $(".popup").hide();
    $(".openpop").click(function (e) {
        e.preventDefault();
        $("iframe").attr("src", $(this).attr('href'));
        $(".links").fadeOut('slow');
        $(".popup").fadeIn('slow');
    });

    $(".close").click(function () {
        $(this).parent().fadeOut("slow");
        $(".links").fadeIn("slow");
    });
});

当然,您必须对样式进行一些更改才能获得更好的视图体验:)

【讨论】:

    【解决方案2】:

    你也可以用这个

    $('#clickme').click( function(){
        $('#showDiv').show();
        $('#iframe').attr('src','your url');
    });
    

    【讨论】:

      【解决方案3】:
      $(document).ready(function() {
          $(".openpop").click(function() {
              var x = $(this).attr('href');
              var y = x.replace('#', '');
              $(".popup").show().html('<iframe src="'+y+'"></iframe>');
              $("iframe").attr("href", y);
          });
      
          $(".close").click(function(){
              $(this).parent().fadeOut("slow");
          }); 
      });
      

      您无法在 iframe 中打开 google 和 yahoo,这两个网站都不允许,请查看此链接 Why Iframe dosen't work for yahoo.com

      但您可以使用此代码打开 w3school

      【讨论】:

      • 嗨,这样不行。X-Frame-Options 拒绝加载:in.yahoo.com/?p=us 不允许框架。 Google 和 Yahoo 的控制台错误
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      相关资源
      最近更新 更多