父页面调用iframe子页面中的方法,报错。
父页面调用:
var mobile = document.getElementById("J-Iframe").contentWindow.getMobile();
alert(mobile);
$("#mobile").text(mobile);
子页面中的方法是:
function getMobile(){
var result = 'null';
return result;
}
报错为:
Uncaught TypeError: document.getElementById(...).contentWindow.getMobile is not a function
前些天也碰到过这个问题,我觉得可能是因为获取iframe的时候还没有加载完成,你绑定一个onload事件试一下:
var frame = document.getElementById("J-Iframe");
frame.onload = function(){
frame.contentWindow.getMobile();
};
javascript单步调试