[英]How to add spans to every word in paragraph of text?
如何將跨度附加到特定文檔div
(“文本”)中的每個單詞? 在js
使用nodes
的js
。 不斷收到消息,
“對象沒有方法附加子項...”
我想念什么? 這是我的代碼:
var y;
var words;
function get() {
y = document.getElementById("text").firstChild.nodeValue;
words = y.split(" ");
for (var x = 0; x < 3; x++)
{
var newSpan = document.createElement('span');
words[x].appendChild(newSpan);
}
}
您需要先獲取要在其后附加單詞的dom元素,
elem = documents.getElementById("#some_container");
然后將單詞附加到它上面
words.forEach(function(w) {
var new_span = document.createElement('<span>');
new_span.innerHTML = w;
elem.appendChild(new_span);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.