HTML page:
1. Add elements in HTML
2. Insert inside the tag
li= document.createElement("li"); li.textContent="第一个元素"; ul.insertAdjacentElement("afterbegin",li); li= document.createElement("li"); li.textContent="最后一个元素"; ul.insertAdjacentElement("beforeend",li);
3. Insert Outside the tag
a=document.createElement("a"); a.innerText="《雪中悍刀行》"; a.href="http://www.baidu.com"; ul.insertAdjacentElement("beforebegin",a); a1=document.createElement("a"); a1.innerText="《剑来》"; a1.href="//m.sbmmt.com/"; ul.insertAdjacentElement("afterend",a1);
Recommended: "2021 js interview questions and answers (large summary)"
The above is the detailed content of How to use Javascript to insert tag elements inside and outside tags. For more information, please follow other related articles on the PHP Chinese website!