日期:2014-05-16  浏览次数:20460 次

关于iframe子页面调用父窗口js函数的问题,请教
这个是父窗口代码:
<html>
<head>
<script type="text/javascript">
function __change(){
console.log('__change()')
}
</script>
</head>
<body>
<iframe src="A/A.html">1</iframe>
</body>
</html>

A.html的代码:
<html>
<head>
<script>
function test(){
console.log('A')
//window.parent.document
// self.parent.__change();
window.parent.__change(); 
// window.parent.__change()
}
</script>
</head>
<body>
<input type="button" onclick="test()" value="A"/>
</body>
</html> 

我在点击按钮A的时候  提示这个错误:
Unsafe JavaScript attempt to access frame with URL file:///U:/js/index.html from frame with URL file:///U:/js/A/A.html. Domains, protocols and ports must match.
 A.html:8
Uncaught TypeError: Property '__change' of object [object Window] is not a function 

今天整了半天   实在是没办法了

javascript iframe function

------解决方案--------------------
哇,这样本地的也算跨域了
你把“A.html”放到和父级页面同目录,或你架起web服务再访问 试试
------解决方案--------------------
什么浏览器?webkit核心的如chrome本地测试访问不了iframe或者iframe访问不了parent对象的,需要搭建服务器通过http访问

本地测试用ie或者firefox:chrome浏览器iframe parent.document为undefined