有时我需要向现有的HTML页面添加元素(例如新链接和图像),但我只能访问远离插入元素位置的页面的一小部分。 我想使用基于DOM的JavaScript技术,并且必须避免使用document.write()。
到目前为止,我一直在使用类似以下的方法:
// Create new image element
var newImg = document.createElement("img");
newImg.src = "images/button.jpg";
newImg.height = "50";
newImg.width = "150";
newImg.alt = "Click Me";
// Create new link element
var newLink = document.createElement("a");
newLink.href = "/dir/signup.html";
// Append new image into new link
newLink.appendChild(newImg);
// Append new link (with image) into its destination on the page
document.getElementById("newLinkDestination").appendChild(newLink);
有没有更有效的方法来完成同样的事情?这些步骤似乎都是必要的,但我想知道是否有更好的方法可以做到这一点。