Javascript 如何为文本段落中的每个单词添加跨距?
如何在特定文档Javascript 如何为文本段落中的每个单词添加跨距?,javascript,append,nodes,html,Javascript,Append,Nodes,Html,如何在特定文档div(“文本”)中的每个单词后面加上span?在js中使用节点的新手。不断收到信息 “对象没有方法附加子对象…” 我错过了什么?这是我的代码: var y; var words; function get() { y = document.getElementById("text").firstChild.nodeValue; words = y.split(" "); for (var x = 0; x < 3; x++)
div
(“文本”)中的每个单词后面加上span?在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);
}
}
vary;
变异词;
函数get(){
y=document.getElementById(“text”).firstChild.nodeValue;
文字=y.分割(“”);
对于(变量x=0;x<3;x++)
{
var newSpan=document.createElement('span');
字[x].追加儿童(新闻潘);
}
}
您需要首先获取要附加单词的dom元素
elem = documents.getElementById("#some_container");
然后在上面加上这些词
words.forEach(function(w) {
var new_span = document.createElement('<span>');
new_span.innerHTML = w;
elem.appendChild(new_span);
});
words.forEach(函数(w){
var new_span=document.createElement(“”);
new_span.innerHTML=w;
附录子元素(新的);
});