<style>
div {
border: 1px solid black;
padding: 5px;
width: 400px;
margin: 0 auto;
}
div h1 {
text-align: center;
margin-top: 0px;
margin-bottom: 0px;
}
section {
border: 1px solid black;
padding: 5px;
}
input[type=text] {
padding: 3px;
width:150px;
height:10px;
}
</style>
<body>
<div id="wrapper">
<h1>Tuesday TODO List</h1>
<input type="text" id="sectionText" class="sectionText" placeholder="Title" />
<button id="btn" class="btn">New Section</button>
</div></body>
var a = document.getElementById('btn');
a.addEventListener('click', function() {
var b = document.createElement('section');
document.getElementById('wrapper').appendChild(b);
}, false);
到目前为止剧本的作品,但我想追加一个孩子,并设置它的ID来var b当我尝试它时,它不起作用。
var d = document.createElement('INPUT');
d.setAttribute('type', 'text').setId('id name');
b.appendChild(d);
这3条线路,当我把他们事件监听器内不工作,我只是把它们隔开外面方便
'd.setAttribute('type','text')'returns'undefined'。你不能像这样在DOM Api中进行链接。 –