/** * insertAdjacentHTML * 支持 insertAdjacentHTML()方法的浏览器有 IE、Firefox 8+、Safari、Opera 和 Chrome */ var element = document.querySelector('#box2') //作为前一个同辈元素插入 element.insertAdjacentHTML("beforebegin", "<p>Hello world!</p>") //作为后一个同辈元素插入 element.insertAdjacentHTML("afterend", "<p>Hello world!</p>") //作为第一个子元素插入 element.insertAdjacentHTML("afterbegin", "<p>Hello world!</p>") //作为最后一个子元素插入 element.insertAdjacentHTML("beforeend", "<p>Hello world!</p>")