往ul里面添加10个li 内容1-10
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<ul id="ul">
</ul>
</body>
</html>
let ul=document.getElementById('ul');
for(let i=0;i<10;i++){
let li=document.createElement('li');
li.innerHTML=i;
ul.appendChild(li);
}
性能优化
let ul=document.getElementById('ul'),
_html='';
for(let i=0;i<10;i++){
_html+=`<li>${i}</li>`
}
ul.innerHTML=_html
createDocumentFragment
let ul=document.getElementById('ul'),
_frag=document.createDocumentFragment();
for(let i=0;i<10;i++){
let li=document.createElement('li');
li.innerHTMl=i;
_frag.appendChild(li);
}
ul.appendChild(_frag);