jQuery 如何从子窗口关闭fancybox?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4390581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close fancybox from child window?
提问by Mohan Ram
parent link:
父链接:
<a href="feedback.php" id="feed">Provide your feedback here</a>
jQuery code to initiate fancybox is...
启动fancybox的jQuery代码是...
$("#feed").fancybox();
codes in feedback.php...
反馈.php中的代码...
<html>
<head>
<script language="javascript">
$(document).ready(function(){
$("#feed_submit").click(function(){
//Name is alerted
alert($("#name").val());
//code to close fancy box(Not working)
$.fancybox.close();
});
});
</script>
</head>
<body>
<form method="post" name="feedback" id="feedback" action="">
<div>
<label>name</label>
<input type="text" name="name" id="name"/>
</div>
<div>
<label>feedback</label>
<input type="text" name="content" id="content"/>
</div>
<div><input type="button" name="feed_submit" id="submit" value="submit"></div>
</form>
</body>
</html>
Once submit button is clicked i need to close fancy box in this page itself with jquery
单击提交按钮后,我需要使用 jquery 关闭此页面中的精美框
i have tried using
我试过使用
$.fancybox.close();
parent.$.fancybox.close();
But this doesn't work for me. If there is any option to close inside this ajax form please let me know.
但这对我不起作用。如果有任何选项可以关闭此 ajax 表单,请告诉我。
回答by dxh
The following code shouldwork, and has worked for many people
以下代码应该可以工作,并且已经为很多人工作
parent.$.fn.fancybox.close();
However, I have also seen, for instance in this question, jQuery being run in noConflict mode, unbeknownst to the user. If this is the case for you, you'd have to modify your script to
但是,我也看到,例如在这个问题中,jQuery 在 noConflict 模式下运行,用户不知道。如果是这种情况,您必须将脚本修改为
parent.jQuery.fn.fancybox.close();
If none of those work, please use a tool like Firebug for Firefox, or the developer console for Chrome, to see if there is an error message, and report back to us what it is.
如果这些都不起作用,请使用 Firefox 的 Firebug 或 Chrome 的开发者控制台之类的工具,查看是否有错误消息,并向我们报告错误消息。
回答by Alin Purcaru
Try
尝试
window.parent.$.fancybox.close();
assuming that$.fancybox.close();
works in the parent window.
假设$.fancybox.close();
在父窗口中工作。
回答by user1758863
Try:
尝试:
$("#fancybox-close").trigger('click');
回答by Naore Azenkut
Faced the same problem, this is the only solution i could finds that did the trick:
面对同样的问题,这是我能找到的唯一解决方案:
the code itself is:
代码本身是:
parent.jQuery.fancybox.close();
it runs in a function as such:
它在一个函数中运行,如下所示:
jQuery("#cancelId").click(function(){
parent.jQuery.fancybox.close();
});
hopes it helps, Naore
希望它有所帮助,Naore
回答by pmdstudio
When you're using fancybox with type: iframe, in the iframe box you have to insert all your libraries like jquery etc. Then to close iframe fancy box just use parent.$.fancybox.close()
当您使用类型为 iframe 的fancybox 时,您必须在 iframe 框中插入所有库,如 jquery 等。然后关闭 iframe 花式框只需使用 parent.$.fancybox.close()
Remember fancybox with type "iframe" opens in new window, fancybox with other types opens in the same window.
请记住,“iframe”类型的fancybox 在新窗口中打开,其他类型的fancybox 在同一窗口中打开。
回答by Henry Quintero
//on child window or iframe put this function:
//在子窗口或iframe上放置这个函数:
function closeIframe(){
parent.closeIframeMain();
}
function closeIframe(){
parent.closeIframeMain();
}
// and this link to close this window
// 以及关闭此窗口的链接
<a onclick="closeIframe();" href="#">Close window</a>
<a onclick="closeIframe();" href="#">Close window</a>
//then on parent window put this function:
//然后在父窗口上放这个函数:
function closeIframeMain(){
jQuery("#fancybox-close").trigger("click");
}
function closeIframeMain(){
jQuery("#fancybox-close").trigger("click");
}
回答by Imran Zahoor
It'll work for sure, just copy and paste it:
它肯定会起作用,只需复制并粘贴它:
<a href="javascript:;" onclick="jQuery('.fancybox-close').fancybox().trigger('click');">Close from Inside</a>
<a href="javascript:;" onclick="jQuery('.fancybox-close').fancybox().trigger('click');">Close from Inside</a>
Actually it triggers close button if fancybox itself, which works in majority of cases.
实际上,如果fancybox 本身,它会触发关闭按钮,这在大多数情况下都有效。
Have fun with programming :)
玩得开心编程:)
回答by Will
I had the same problem, but with ASP.NET.
我有同样的问题,但使用 ASP.NET。
Basically, on a page, I'm opening a page (with a form control inside) like this:
基本上,在一个页面上,我打开一个页面(里面有一个表单控件),如下所示:
$.fancybox({
href: '/ContactUs.aspx',
type: 'iframe',
padding: 60,
width: 465,
height: 670,
maxWidth: 465,
maxHeight: 670,
});
When I click close on the FancyBox, it works, but if I submit it first THEN close it, the fancyBox
won't close, even thou the page is in the same domain.
当我在 FancyBox 上单击关闭时,它可以工作,但是如果我先提交然后关闭它fancyBox
,即使页面在同一个域中,也不会关闭。
In the end, I found out you must set autoSize:false when you open the FancyBox:
最后,我发现你必须在打开 FancyBox 时设置 autoSize:false:
$.fancybox({
href: '/ContactUs.aspx',
type: 'iframe',
padding: 60,
width: 465,
height: 670,
maxWidth: 465,
maxHeight: 670,
autoSize: false
});
It should now work. Please reply if this works for anyone else. Cheers.
它现在应该可以工作了。如果这对其他人有效,请回复。干杯。
Will
将要
回答by Poelinca Dorin
$.fn.fancybox.close();
does this work ?
这行得通吗?
回答by publikz.com
parent.jQuery.fancybox.close(); with .$. - creashes in IE8. With this - all ok.
parent.jQuery.fancybox.close(); 用 .$. - 在 IE8 中出现折痕。有了这个 - 一切都好。