<分区>
你好 StackOverflow 社区,
我的 <iframe>
有问题和JavaScript。假设我有一个带有 iframe
的 index.html在 body 里。 index.html
内我在 <head>
中有这个标签:
<script type="text/javascript">
function alertTest() {
alert("Testing");
}
</script>
在 iframe 的 src="iframe.htm"
中(该位置与 index.html 是同一个域)。在里面 iframe.htm
我的页面:
<a href="javascript:void(0);" onclick="window.parent.alertTest();">Click here.</a>
问题是它没有加载警报并且在 Google Chrome 中返回错误:
"Uncaught TypeError: Property 'alertTest' of object [object DOMWindow] is not a function
我试过了window.parent.alertTest();
, window.top.alertTest();
, top.alertTest();
, parent.alertTest();
以及我搜索过的几乎所有关于类似问题的内容。
您似乎无法从位于所述非 iframe 中的 iframe 调用位于非 iframe 中的函数。
请帮忙!
附言本质上,在实际的最终结果中,我试图调用一个 Lightbox 打开图像函数(使用 slimbox,因为我能够从 javascript 而不是从 href 调用打开图像函数)。我还使用 target="_parent"属性尝试了这个最终结果。