2011-09-30 96 views

回答

14

我想你想的..

//html 
<div id="container"> 
<input id="test" type="text" > 
<input value="add" type="button" onClick="add()"> 
</div> 

//js 
<script> 
var i=0; 
function add(){  
    if (document.getElementById('test').value!='') 
    { 
     i++; 
     var title =document.getElementById('test').value; 
     var node = document.createElement('div');   
     node.innerHTML = '<input type="checkbox" id="check' + i + '" name="check' + i + '"><label for="check' + i + '">'+ title +'</label>';  
     document.getElementById('container').appendChild(node);  
    } 
} 
</script> 
16

對於標籤試試這個:

var newlabel = document.createElement("Label"); 
newlabel.setAttribute("for",id_from_input); 
newlabel.innerHTML = "Here goes the text"; 
parentDiv.appendChild(newlabel); 
相關問題