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

用JS动态生成html,报错,第一次发帖,虚心求教!

<html>
<head>
    <title>New Document </title>
    <script type="text/javascript">

var inputtext=document.createElement("input");
var inputbutton=document.createElement("input");
inputtext.type="text";
inputtext.id="text";
inputbutton.type="button";
inputbutton.id="button";
inputbutton.value="按钮";
//inputbutton.onclick="button_onClick();";

console.error(inputtext);
console.error(inputbutton);
document.body.appendChild(inputtext);
document.body.appendChild(inputbutton);
function button_onClick(){
            var txt = text.value;
            var num = parseInt(txt);
            for(var i=0;i<num;i++){  
                 var element = document.createElement("A");
                 element.setAttribute("href","");
                 var br = document.createElement("BR");         
                
                 var a = document.createTextNode(i+1);
                
                 element.appendChild(a); 
                 
                 document.body.appendChild(br);
                 document.body.appendChild(element); 
            }                                                              
        }       
    </script>     
</head>
<body>
</body>
</html>
html js动态生成html

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht