JS作成HTMLタグ

1697 ワード

<script type="text/javascript">
var count = 0;
function Add(){
   count += 1;
   var File1 = document.getElementById("file1");
   var div = document.createElement("div");
   var btnJiaHao = document.createElement("input");
   btnJiaHao.type = "button";
   btnJiaHao.value = "+";
   btnJiaHao.id = count;
   btnJiaHao.onclick = function(){
   		jiahao(this);	
   }
   var inputTxt = document.createElement("input");
   inputTxt.type = "text";
   inputTxt.name = "txt"+count;
   inputTxt.id = "txt_"+count;
   inputTxt.value = "0";
   var btn = document.createElement("input");
   btn.type = "button";
   btn.value = "-";
   btn.id = count;
   btn.onclick = function(){
       jianhao(this);
   }
  
   div.appendChild(btnJiaHao);
   div.appendChild(inputTxt);
   div.appendChild(btn);
   File1.appendChild(div);
}
function jianhao(t){
	var txt_id = "txt_" + t.id;
	var v = document.getElementById(txt_id).value;
	v = v - 1;
	document.getElementById(txt_id).value = v;
}
function jiahao(t){
	var txt_id = "txt_" + t.id;
	var v = document.getElementById(txt_id).value;
	v = parseInt(v) + 1;
	document.getElementById(txt_id).value = v;
}
</script>

<input type="button" name="button" value="  " onclick="Add();" />
<div id="file1"></div>