jQuery 如何在 iframe 中关闭颜色框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2101011/
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 colorbox within iframe?
提问by Ashish Rajan
i have a page manageGroup.php, where user can also add member to group. I used colorbox to show the addGroupMember.php. Now i need to close that colorbox once i have done submitting the form.
我有一个页面 manageGroup.php,用户还可以在其中将成员添加到组中。我使用 colorbox 来显示 addGroupMember.php。现在我需要在完成提交表单后关闭该颜色框。
javascript i am using in manageGroup.php
我在 manageGroup.php 中使用的 javascript
<script language="javascript" type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/dropdown.js"></script>
<script type="text/javascript" src="js/jquery.colorbox.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".iframe").colorbox({width:"80%", height:"80%", iframe:true});
});
</script>
The link i am using to open colorbox
我用来打开颜色框的链接
<a class="iframe" href="addGMember.php?id=<?php echo base64_encode($fetch->g_id)?>">Add Member</a>
the code in addGroupMember.php is like this:-
addGroupMember.php 中的代码是这样的:-
if($_POST['add']=="Go")
{
$gid = $_POST['id'];
$ii=0;
$insert = "INSERT INTO ".DBGMEMBER." (gm_g_id,gm_m_id) VALUES ";
foreach($_POST['gMember'] as $gMember)
{
if($ii==0)
{
$insert .= " ('".$gid."' , '".$gMember."')";
}
else
{
$insert .= " ,('".$gid."' , '".$gMember."')";
}
$ii++;
}
$db->execute($insert);// after this i want to close the colorbox
echo "<script>parent.$.fn.colorbox.close(); </script>";// code i used, but not working
}
回答by Mr_Nizzle
This one worked out perfectly for me and should work for you
这个对我来说很完美,应该对你有用
parent.jQuery.colorbox.close()
回答by Ashish Rajan
i got it done for me, a bit crazy way, anyways u can too give it a try.
我为我完成了,有点疯狂,无论如何你也可以尝试一下。
Supposing your page in iframe as x.php having form named xyz
假设您在 iframe 中的页面作为 x.php 具有名为 xyz 的表单
<?php
if($_post['submit']=='Submit')
{
//some php code here
if(success)
echo "<script>parent.$.fn.colorbox.close(); </script>";
else
{
//some error handling here;
}
}
?>
<form name='xyz' action='x.php'>
//some html code here
<input type='Submit' name='submit' />
</form>
回答by fredrik
First: Elaborate you question. The information you provided is some what shorthanded. There's no chance one could grip what you are doing. Also include some more sample code.
第一:详细说明你的问题。您提供的信息有些不足。没有人能抓住你正在做的事情。还包括一些更多的示例代码。
Only thing I could guess is that you trying to trigger the method in how it's written. Everything you add to the $.fn object is bound to all jQuery objects.
我唯一能猜到的是你试图以它的编写方式触发该方法。您添加到 $.fn 对象的所有内容都绑定到所有 jQuery 对象。
// doesn't work
$.fn.colorbox.close()
// proper way
$('idOfDomElement').colorbox.close()
..fredrik
..弗雷德里克
回答by JustinJason
Make sure that the page loaded within the iFrame. All of the necessary references to the colorbox js, jQuery js are not needed.
确保页面在 iFrame 中加载。不需要对 colorbox js、jQuery js 的所有必要引用。
Then the parent close call will work:
然后父关闭呼叫将起作用:
parent.$('.yourElement').colorbox.close();
回答by steve
just this
只是这个
parent.$.colorbox.close()
seems to work for me!
似乎对我有用!
I used it like this without even a reference to any JS in the file loaded into the iframe.
我像这样使用它,甚至没有引用加载到 iframe 的文件中的任何 JS。
<a href="javascript:parent.$.colorbox.close()">close</a>
this alone in a blank HTML file seems to work.
这在一个空白的 HTML 文件中似乎是可行的。
回答by rets
I think the problem is that the colorbox belongs to the parent, not to the DOM in the iframe.
我认为问题在于颜色框属于父级,而不是 iframe 中的 DOM。
My guess is you'll need to call parent.[way to get the element or $.fn object].colorbox.close() or you'll need to add a function to the parent document and call parent.myCloseFunction()
我的猜测是你需要调用 parent.[获取元素或 $.fn 对象的方法].colorbox.close() 或者你需要向父文档添加一个函数并调用 parent.myCloseFunction()
回答by Ahmet
There is a simple solution actually which i use.
实际上我使用了一个简单的解决方案。
i put this
<a id="btnDone" href="#">here</a>
inside code or you can use your submit button's id and
我把它
<a id="btnDone" href="#">here</a>
放在代码里面,或者你可以使用提交按钮的 id 和
$(document).ready(function () {
$("#btnDone").click(function () {
window.parent.location = "../siparisler/listele.aspx"
});
}
this way u can redirect the parent page. and colorbox is automatically close. if you dont need redirection just put "#" for the window.parent.location
这样你就可以重定向父页面。并且 colorbox 会自动关闭。如果您不需要重定向,只需为 window.parent.location 放置“#”
回答by David
I just tried to close colorbox from within the iframe but couldn`t get it to work. I used $('#closebox').colorbox.close() and without any luck.
我只是试图从 iframe 中关闭 colorbox,但无法让它工作。我使用了 $('#closebox').colorbox.close() 并且没有任何运气。
回答by Dr Casper Black
I did not manage to close the Colorbox with suggested solutions. I get parent undefined... So..
我没有设法使用建议的解决方案关闭 Colorbox。我得到父母未定义......所以......
I found took a different approach:
我发现采取了不同的方法:
Home document: (iframe: false) this will open the ajax_page in DIV in the same document
主页文档:(iframe: false)这将在同一文档中的 DIV 中打开 ajax_page
$(document).ready(function(){
$("#modalwindow").colorbox({
width:"800px",
height:"510px",
iframe:false,
href:"../ajax_page.php",
});
});
and in the ajax_page
并在 ajax_page
$('#modalwindow').colorbox.close();
Hope this helps
希望这可以帮助