Dom-创建标签

时间:2022-07-01 19:12:44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="i1">
<input type="button" value="+" onclick="add_1();" style="background-color: red">
<input type="button" value="+" onclick="add_2()" style="background-color: yellow">
</div>
<script>
function add_1(){
var tag = "<p><input type='text' style='background-color:red' /></p>" ;
document.getElementById('i1').insertAdjacentHTML('beforeEnd',tag);
}
function add_2(){
var tag = document.createElement('input');
tag.setAttribute('type','text')
tag.style.backgroundColor ='yellow';
var p1 = document.createElement('p');
p1.appendChild(tag)
document.getElementById('i1').appendChild(p1)
}
</script>
</body>
</html>