Dynamically create the JavaScript code for the button

  • 2020-12-09 00:43:28
  • OfStack

Without further ado, I will post js code directly to you. The specific code is as follows:


<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Document</title>
<script type="text/javascript">
var i = 0;
function addInput(){
var o = document.createElement('input');
o.type = 'button';
o.value = ' button '+ i++;
if(o.attachEvent){
o.attachEvent('onclick',addInput)
}else{
o.addEventListener('click',addInput)
}
document.body.appendChild(o);
o = null;
}
</script>
</head>
<body onload="addInput()">
</body>
</html>

This is the JavaScript code for creating buttons dynamically, which I hope will be helpful to you.


Related articles: