javascript - Jquery 中的 iframe 未加载页面

标签 javascript jquery iframe

我试图用 Jquery 尝试构建一个 iframe,我认为通过这段代码可以直接实现,但它不起作用..

jQuery(document).ready(function() {
              $("#opposition a").click(function(e) {
              var first_id  = $(this).attr('id');
              var second_id = $("h1").attr('id');
              $("#opposition img").mouseover(function() {
              $(this).css('border-width','5px');
        });
              $("#opposition img").mouseout(function() {
              $(this).css('border-width','2px');
        });
              $.get("compare_proc.php",{
              id:first_id,
             id2:second_id
    });
     $("#frame").attr("src", "http://www.google.com/");
              e.preventDefault();
                });
    });

HTML:

 <div id="mydiv"><iframe id="frame" src="" width="100%" height="300">
   </iframe></div>
  <button id="button">Load</button>

最佳答案

这是因为 Google.com 响应的 header 名为 X-Frame-Options 。来自文档:

The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a <frame> or <iframe>. Sites can use this to avoid clickjacking attacks, by ensuring that their content is not embedded into other sites.

并非所有服务器都会发送此响应 header ,例如( demo ),这有效:

<div id="mydiv"><iframe id="frame" src="" width="100%" height="300">
   </iframe></div>
  <button id="button">Load</button>
<script>
$('#button').on('click', function() {
    $("#frame").attr("src", "http://wikipedia.com");
});
</script>

关于javascript - Jquery 中的 iframe 未加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16289869/

相关文章:

javascript - 如何: On click open new page in new tab and redirect the old one

javascript - JSON 响应 Long 被舍入或损坏

javascript - jQuery Waypoints 仅在元素到达窗口顶部时触发

javascript - 如何在 jQuery 中设置表单提交时间限制

javascript - Fancybox iframe 不出现

javascript - 如何使用 JQuery 而不是 Javascript 显示警报消息?

javascript - 让 Webpack 在构建时警告缺少包

php - 使用 Twitpic/Yfrog 从网站发布图像

iframe - 如何检测用户是否登录了 flickr?

html - iframe 在容器内居中并停止调整大小