Javascript分隔符新行

时间:2015-10-31 20:30:07

标签: javascript

我需要根据用户输入的分隔符拆分一个单词,然后将它们显示在一个div中,每个单词都在一个单独的行上。我不知道如何在不同的行上显示每个分隔的单词。这是代码:

function customBreakIt(){
var text = document.getElementById("textbox").value; //grabs text to break
var location = document.getElementById("textbox2").value; //grabs user entered delimiter
var list = text.split(location);    //splits the text at the delimiter   
var recombined = ""; 
var newLine = document.createElement("br");
for(x in list){         
    recombined = recombined + list[x];        
    document.getElementById("second").innerHTML = recombined; //changes the <p> text in an ID called 'second' to recombined
    //now I need to also let each new word appear on a separate line
              }
}

2 个答案:

答案 0 :(得分:0)

您可以为每个单词document.createElement("p")添加文本到新的<p>元素,并将新元素附加到第二个容器。

答案 1 :(得分:0)

您可以将重组列表封装到div中并连接输出:

//var newLine = document.createElement("br");
for (x in list) {         
    recombined += '<div>' + list[x] + '</div>';
}
document.getElementById("second").innerHTML = recombined;