Jquery 如何从iframe内部关闭iframe?
我有一个Wordpress网站,帖子被加载到iframe中 这是有效的代码:Jquery 如何从iframe内部关闭iframe?,jquery,wordpress,iframe,Jquery,Wordpress,Iframe,我有一个Wordpress网站,帖子被加载到iframe中 这是有效的代码: <a class="trick" rel="<?php the_permalink() ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a> $(文档).ready(函数(){ $.ajaxSetup({cache:false}); $(“.trick”)。单击(函数(){ var p
<a class="trick" rel="<?php the_permalink() ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a>
$(文档).ready(函数(){
$.ajaxSetup({cache:false});
$(“.trick”)。单击(函数(){
var post_link=$(this.attr(“rel”);
$(“#frame”).css(“显示”、“块”);
$(“#frame”).attr(“url”,post#u链接);
$(“body”).css(“溢出”、“隐藏”);
});
});
现在,如何从iframe内部关闭此加载的iframe
主页是index.php(主wordpress循环),iframe的内容是single.php(single post),没有页眉和页脚
谢谢
这就是我在single.php中得到的
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("#close").click(function(){
$('#frame', window.parent.document).remove();
});
});
</script>
</head>
<body>
<div id="container-single">
<button id="close" >Close</button>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article <?php post_class('single') ?> id="post-<?php the_ID(); ?>">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>
<?php the_tags( 'Tags: ', ', ', ''); ?>
<?php include (TEMPLATEPATH . '/_/inc/meta.php' ); ?>
</div>
</article>
<?php endwhile; endif; ?>
</div>
</body>
$(文档).ready(函数(){
$(“#关闭”)。单击(函数(){
$('#frame',window.parent.document).remove();
});
});
接近
从single.php
执行以下代码,该代码加载在iframe
中。这将使用父窗口作为上下文查找iframe
,并将其删除或隐藏
//You can call hide() if you want to just hide it
$('#iframe', window.parent.document).remove();
其实我知道一个小把戏
在父页面上创建函数
var closeIFrame = function() {
$('#iframeid').remove();
}
在iframe中,您可以在任何地方关闭呼叫
parent.closeIFrame();
很棘手,不是吗?我在创建书签时遇到了一个问题,就像Pinterest的Pin一样
// Inside the iframe
frameElement.remove();
它应该跨域工作
我能解决这个问题的唯一方法是在iframe内的页面和GitHub上的父页面之间发布事件,如下示例所示:
我在另一个帖子上发布了一个答案:
希望有帮助 关闭Iframe?什么意思?你想删除内部iframe还是什么?如果你声明这样一个函数,但它没有添加到窗口
对象中,那么它可能会重复,我相信你必须这样做:var closeIFrame=function(){…}
我发现这个答案很有用。这是我搜索了几个小时的解决方案。我已经尝试过了,但没有成功,你可以在我的问题中看到我的single.php内容被编辑过…只是要确定一下。iframe在同一个域中吗?是的,当然。iframe加载wordpress帖子。然后它应该找到iframe并关闭它。你能看到console.log和$('#iframe',window.parent.document)
提供了什么吗?console.log没有提供任何东西。。没错,我在single.php中添加了什么?
parent.closeIFrame();
// Inside the iframe
frameElement.remove();