这样就 都执行了<!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>无标题文档 </title> 
</head> 
<script language="Javascript" type="text/JavaScript" defer="defer"> 
function show() { 
var box,boxSubmit; box = document.getElementById("box"); boxSubmit = document.createElement("input"); 
boxSubmit.setAttribute("type","button"); 
boxSubmit.setAttribute("value","执行"); 
boxSubmit.onclick=function(){
alert("给我执行啊~!");
}
//boxSubmit.setAttribute("onClick","javascript:alert(\"给我执行啊~!\");"); box.appendChild(boxSubmit); 
} </script> 
<body onload="show();"> 
<div id="box"> </div> 
</body> 
</html> 

解决方案 »

  1.   

    也可这样 传引用
    <!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>无标题文档 </title> 
    </head> 
    <script language="Javascript" type="text/JavaScript" defer="defer"> 
    function show() { 
    var box,boxSubmit; box = document.getElementById("box"); boxSubmit = document.createElement("input"); 
    boxSubmit.setAttribute("type","button"); 
    boxSubmit.setAttribute("value","执行"); boxSubmit.onclick=myOnclick;
    box.appendChild(boxSubmit); 

    function  myOnclick(){
    alert("给我执行啊~!");
    }</script> 
    <body onload="show();"> 
    <div id="box"> </div> 
    </body> 
    </html>