innerText是把对象内的所有内容都替换为文本,所以如果先添加radio,然后再用innerText,radio就会自然消失,被文本取代。
应该用appendChild的方式来添加文本,即:
var label = document.createElement("label")
var radio = document.createElement("input")
radio.type = "radio"
radio.name = "radio"// 名字随意
label.appendChild(radio)
var txt = document.createTextNode("一个radio")
label.appendChild(txt)
document.getElementById("Div").appendChild(label)// Div为父容器id
最后补充说一句:innerHTML和innerText 跟 appendChild 是两种不同的机制,最好不要混用!!!
var radio=document.createElement('input')radio.type="radio"
radio.name="radio1"
radio.value="学生"
radio.checked="checked"
document.body.appendChild(radio)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)