src
of an <img>
:imgEl.src = "http://www.dogs.com/dog.gif";
setAttribute
method, like so:imgEl.setAttribute("src", "http://www.dogs.com/dog.gif");
removeAttribute
- like to remove the disabled
attribute off a button, effectively enabling it:imgEl.removeAttribute("disabled");
style
property of the element, and setting the corresponding property. For example, to change the color:headingEl.style.color = "hotpink";
headingEl.style.backgroundColor = "salmon";
className
property:mainEl.className = "warning";
mainEl.className += " warning";
classList
functionality instead:mainEl.classList.add("warning");
innerHTML
:mainEl.innerHTML = "cats are the <strong>cutest</strong>";
textContent
instead:mainEl.textContent = "cats are the cutest";
createElement
:var imgEl = document.createElement("img");
appendChild
on the target parent element:document.body.appendChild(imgEl);
Summary: DOM modification techniques
原文:https://www.cnblogs.com/ruruozhenhao/p/8998653.html