1.
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <title>动态创建按钮</title> 7 <script language="javascript"> 8 var i=0 ; 9 <!-- 10 attachEvent和addEventListener 支持的浏览器有区别 11 --> 12 function addInput() 13 { 14 var input =document.createElement("input"); 15 input.type="button"; 16 input.value="动态按钮"; 17 /* input.attachEvent("onclick",function(){ 18 alert("aaaa"); 19 }); */ 20 input.addEventListener('click',function(){ 21 alert("bb"); 22 }); 23 document.body.appendChild(input);//添加按钮 24 } 25 </script> 26 </head> 27 <body onload="addInput();"> 28 <input type="button" value="abc" onclick="addInput();"> 29 </body> 30 </html>