添加 <br> or <p> 与 createElement



如何在表前后添加createElement(br)?最后我用doc.body.appendChild(p);试了一下,但没有任何反应。我不知道我必须在哪里写appendchild

function insertBlock(border) 
{       
    var doc = document.getElementById("frame").contentWindow.document;
    var p = doc.createElement("br");            
    var range = doc.getSelection().getRangeAt(0);
    myParent=document.getElementById("frame").contentWindow.document.body;
    if (border == true)
    {
        myTable=document.createElement("table");
        myTable.setAttribute("style","border: 1px solid #000000;");
    }
    else
    {
        myTable=document.createElement("table");
    }
    // IE5, IE6 benoetigen unbedingt tbody-Element
    myTBody=document.createElement("tbody");
    myRow=document.createElement("tr");
    myCell=document.createElement("td");
    myText=document.createTextNode("Die erste Zelle");
    myCell.appendChild(myText);
    myRow.appendChild(myCell);
    myCell=document.createElement("td");
    myText=document.createTextNode("Die zweite Zelle");
    myCell.appendChild(myText);
    myRow.appendChild(myCell);
    myTBody.appendChild(myRow);
    myTable.appendChild(myTBody);
    myParent.appendChild(myTable);
    range.insertNode(myTable);
}   
应该

在这里:

myParent.appendChild(p);
myParent.appendChild(myTable);
myParent.appendChild(p.cloneNode());

请注意,第二个是克隆。当它不是。只会添加第二个。(不能将元素添加两次)。

当然,您也可以添加两个不同的对象。

myParent.appendChild(p);
myParent.appendChild(myTable);
myParent.appendChild(p2);

或者没有变量:

myParent.appendChild(doc.createElement("br"));
myParent.appendChild(myTable);
myParent.appendChild(doc.createElement("br"));

试试这个:

myTable.insertAdjacentHTML('afterend','<br />');
myTable.insertAdjacentHTML('beforebegin','<br />');

如果您想使用 createElement('br') .

var p = doc.createElement("br");            
myParent.insertBefore(p,myTable);
myParent.insertBefore(p.cloneNode(),myTable.nextSibling);

最新更新