简体   繁体   English

成功的br元素

[英]Succesive br Elements

function formatResults(){

    $("#answersToSearch").empty();
    var i=0;
    for(;i<answers[1].length;i++){

      var title=document.createTextNode(answers[1][i]);
      var desc=document.createTextNode(answers[2][i]);
      var newLine=document.createElement("br");

      document.getElementById("answersToSearch").appendChild(title);
      document.getElementById("answersToSearch").appendChild(newLine);

      var newLine=document.createElement("br");
      document.getElementById("answersToSearch").appendChild(newLine);

      document.getElementById("answersToSearch").appendChild(desc);
      var newLine=document.createElement("br");
      document.getElementById("answersToSearch").appendChild(newLine);
      var newLine=document.createElement("br");
      document.getElementById("answersToSearch").appendChild(newLine);


    }  

}

Initially, I tried using the same variable 'newLine' representing a new line for carriage returns. 最初,我尝试使用相同的变量'newLine'来表示回车的新行。 But it works only once. 但它只工作一次。 Then i tried declaring it each time i used a carriage return and it worked. 然后我尝试在每次使用回车时声明它并且它有效。 So my question is - Why do i have to declare a new 'br' element each time to add a carriage return. 所以我的问题是 - 为什么我每次都要声明一个新的'br'元素来添加回车。 Or is there something that I have done wrong??? 或者有什么我做错了???

Node.appendChild() method adds a node to the end of the list of children of a specified parent node. Node.appendChild()方法将节点添加到指定父节点的子节点列表的末尾。 If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position 如果给定子节点是对文档中现有节点的引用,则appendChild()将其从当前位置移动到新位置

(Emphasis added) Source (重点补充) 来源

appendChild() does not clone the node. appendChild()不克隆节点。 You could clone the node yourself: 您可以自己克隆节点:

appendChild(newLine.cloneNode(true));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM