textContent
替换元素中的所有内容。
http://jsfiddle.net/2nmL7v5b/6/
你必须像这样
var infoDiv = document.createElement("div");
infoDiv.setAttribute("class", "text-block");
var bio = document.createElement("strong");
bio.textContent = "Bio";
infoDiv.appendChild(bio);
var spanElem = document.createElement("div");
spanElem.textContent = "Full";
infoDiv.appendChild(spanElem)
document.getElementsByTagName("body")[0].appendChild(infoDiv)
<body>
</body>
或者,如果你不想使用span
标签,你可以使用innerHTML
也喜欢
http://jsfiddle.net/2nmL7v5b/11/
var infoDiv = document.createElement("div");
infoDiv.setAttribute("class", "text-block");
var bio = document.createElement("strong");
bio.textContent = "Bio";
infoDiv.appendChild(bio);
infoDiv.innerHTML += "Full";
document.getElementsByTagName("body")[0].appendChild(infoDiv)
<body>
</body>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句