在Javascript中的单元格内创建div

在Javascript中的单元格内创建div,javascript,createelement,Javascript,Createelement,在使用Javascript动态创建单元格后,我在创建div时遇到问题。我的目标是能够添加完全相同的原始表行及其下面的内容。以下是HTML代码: <table width="100%" id="processTable"> <tr> <td id="ProcessDetails"><div id="description">Replace with description.</div> <div

在使用Javascript动态创建单元格后,我在创建div时遇到问题。我的目标是能够添加完全相同的原始表行及其下面的内容。以下是HTML代码:

      <table width="100%" id="processTable">
    <tr>
    <td id="ProcessDetails"><div id="description">Replace with description.</div>
    <div id="QuestionToAnswer"><b>Replace with a question answerable by YES or NO</b></div>
    </td>
        <td id="AvailableAnswersColumn">
        <p id="option1"><a href="#YES">YES</a></p>
        <p id="option2">NO: Proceed to next question</p>
        </td>
    </tr>
<!--Insert new table row if needed-->
    </table>
    <div id="footer">
    <input type="button" value="Insert Table Row" id="CreateRow" class="CreateRow" onclick="insertRow()" />
    </div>

替换为描述。
替换为回答为是或否的问题

否:继续下一个问题

下面是Javascript

<script>
function insertRow() {
    var table = document.getElementById("processTable");
    var row = table.insertRow(1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = //within this cell should be created the div ids "description" + "QuestionToAnswer";
    cell2.innerHTML = //within this cell should be created the paragraph with ids "option1" + "option2";;
    cell1.setAttribute("id", "ProcessDetails", 0);
    cell2.setAttribute("id", "AvailableAnswersColumn", 1);
}
</script>

函数insertRow(){
var table=document.getElementById(“processTable”);
var行=table.insertRow(1);
var cell1=行插入单元格(0);
var cell2=行插入单元格(1);
cell1.innerHTML=//应在该单元格内创建div id“description”+“QuestionToAnswer”;
cell2.innerHTML=//应在该单元格内创建ID为“option1”+“option2”的段落;;
cell1.setAttribute(“id”,“ProcessDetails”,0);
cell2.setAttribute(“id”,“AvailableAnswersColumn”,1);
}
请帮忙。

将是您在这里的朋友

var div = document.createElement("div");
div.innerHTML = "Replace with description.";
cell1.appendChild(div);

使用
document.createElement()
可以使用JavaScript代码创建任何想要的html元素。您可以使用
appendChild
将其附加到单元格中。因为在我的示例中,
div
是一个对象,并且在附加DOM节点之后是对它的引用,所以您可以为它设置事件处理程序等。

setAttribute
不接受第三个参数。您要传递的
0
1
是什么?谢谢!正是我需要的