Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为文本段落中的每个单词添加跨距?_Javascript_Append_Nodes_Html - Fatal编程技术网

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;
附录子元素(新的);
});