小弟没怎么学过JS,请各位大虾帮我看看下面这段代码怎么改啊?谢谢啦!<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Untitled Document</title>
    </head>
    <script language="JavaScript">
        
        function method1(){
alert("ddd");
            var o = new Object();
            o.id = 1;
            o.name = "hello";
            var innerhtml = '<input type="button" onClick="method2(' + obj + ')" value="click me">';
innerhtml+='<br>hello';
            document.getElementById("div").innerHTML = innerhtml;
        }
        
        function method2(obj){
            alert(obj.name);
        }
        
    </script>
    <body onload="method1()">
        <div id="div">
        </div>
    </body>
</html>

解决方案 »

  1.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    <script language="JavaScript">function method1(){
    alert("ddd");
    var o = new Object();
    o.id = 1;
    o.name = "hello";
    var innerhtml = '<input type="button" id="btn"  value="click me">';
    innerhtml+='<br>hello';
    document.getElementById("div").innerHTML = innerhtml;
    document.getElementById("btn").onclick=function(o){ return function(){method2( o )}}(o);}function method2(obj){
    alert(obj.name);
    }</script>
    <body onload="method1()">
    <div id="div">
    </div>
    </body>
    </html>