你运行到17行,b.html还没加载呢,就获取不到了

解决方案 »

  1.   

    1.在加载完后再执行
    2.是button.onclick=function(),不是button.click=function()
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>a</title>
    <script type="text/javascript">
        function alert_(){
            var iframe=document.getElementById("frame").contentWindow;   
            alert(iframe.document.getElementById("div").innerHTML);
        }
         
        function update_(){
            var iframe=document.getElementById("frame").contentWindow;   
            iframe.document.getElementById("div").innerHTML="七上八下";
        }
        function doit(){
        //给b页面绑定点击事件    这里为什么不行呢  求解决
        var button=document.getElementById("frame").contentWindow.document.getElementById("button2");
        button.onclick=function(){
            alert("555555555");   
        }
        }
         
    </script>
    </head>
     
    <body onload="doit()">
    <div>
    <input type="button" value="弹出iframe中div的值" onclick="alert_()" id="button1"/>
    <input type="button" value="修改iframe中div的值" onclick="update_()"/>
    </div>
    <iframe src="b.html" frameborder="1" width="600" height="100" style="border:#F00" id="frame"/>
    </body>
    </html>
      

  2.   


    <iframe src="b.html" frameborder="1" width="600" height="100" style="border:#F00" id="frame"/>
    <script type="text/javascript">
    var button=document.getElementById("frame").contentWindow.document.getElementById("button2");
        button.onclick=function(){
            alert("555555555");   
        }
    </script></body>