我试图在iframe内调用2个函数setListener();
和addNewBox();
。无法从iframe调用父函数
我到处寻找,并尝试了一切,我无法得到它的工作。
我没有权限访问父项的头部,因为它是通过PHP需求在块中构建的。
这是父文件外观的粗略模型。
<head>
<?php require "head.html" ?>
</head>
<body>
<?php require "content.html" ?>
</body>
content.html样机:
<!-- a bunch of boring divs -->
<iframe src="image_editor.html"></iframe>
<script> /* random ajax */ </script>
<script>
$(document).ready(function(e) {
//init
setListener();
addNewBox();
function setListener()
{
//MAGIC HAPPENS
}
function addNewBox()
{
//GREATER MAGIC HAPPENS
}
}
</script>
image_editor.html样机:
<body>
<form id="image_form">
<input type="submit" value="Submit"/>
</form>
<script>
$(document).ready(function(e) {
$("#image_form").submit(function(e) {
//MAGIC HAPPENS HERE
//re-adds listeners
window.parent.addNewBox();
window.parent.setListener();
}
</script>
</body>
我已经尝试每一个解决方案,我可以在网上找的,从使用上面,再声明在公共格式中的父函数(window.myFunction = function(){};)。
我将不胜感激任何意见。
感谢
UPDATE: 一些调试后,它似乎调用不工作的原因是因为他们执行前停止一行。不知何故这行“$(”。new“,window.parent.document).remove();”打破了一切。我现在必须找出那一个。我感谢你的帮助。
看看我的答案! –