我試圖在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();」打破了一切。我現在必須找出那一個。我感謝你的幫助。
看看我的答案! –