Javascript<;退货>;:未定义

Javascript<;退货>;:未定义,javascript,return,undefined,Javascript,Return,Undefined,我试图用Javascript将canvas放在iframe元素中,但当我执行代码时,我从调试程序得到了:undefined 这是密码有人能帮我吗 function submitTryit() { var ifr = document.createElement("iframe"); ifr.setAttribute("id", "iframeResult"); document.getElementById("iframewrapper").innerHTML = "";

我试图用Javascript将canvas放在iframe元素中,但当我执行代码时,我从调试程序得到了:undefined 这是密码有人能帮我吗

function submitTryit() {
    var ifr = document.createElement("iframe");
    ifr.setAttribute("id", "iframeResult");
    document.getElementById("iframewrapper").innerHTML = "";
    document.getElementById("iframewrapper").appendChild(ifr);

    canvas();
}

function canvas(){
    var ifrr = document.getElementById("iframeResult");
    var iframediv = (ifrr.contentWindow.document || ifrr.contentDocument.document);
    var canv = document.createElement('canvas');
    canv.setAttribute("id", "mycanvas");
    iframediv.body.appendChild(canv);
}

你所有的代码都很好,问题是你运行得太快了;您试图在页面上甚至不存在iframewapper之前获取它。解决方案很简单,在确定页面已加载之前,不要调用
submitTryIt()

window.onload = function()
{
  submitTryit();
}
window.onload=function(){
submitTryit();
}
函数submitTryit(){
var ifr=document.createElement(“iframe”);
ifr.setAttribute(“id”、“iframesult”);
document.getElementById(“iframewrapper”).innerHTML=“”;
document.getElementById(“iframewrapper”).appendChild(ifr);
画布();
}
函数画布(){
var ifrr=document.getElementById(“iframeResult”);
var iframediv=(ifrr.contentWindow.document | | ifrr.contentDocument.document);
var canv=document.createElement('canvas');
canv.setAttribute(“id”、“mycanvas”);
iframediv.body.appendChild(canv);
}


您是否可以提供堆栈跟踪等。如果您提供堆栈跟踪,其他人会帮助您。显示此页面的相关html。调试器指向哪一行代码?如果iframediv为null,则返回未定义的代码。你还应该提供html来双重检查一切是否正常只是为了获取信息你是对的我只是继续写我的函数,一切都正常谢谢