我想知道如何将HTML代码放入JavaScript函数中,如单击按钮时显示的表单代码。 我已经学会了如何通过点击按钮将行添加到表中,但是没有涉及到的HTML代码不能解决我的问题。这里是我使用的代码:在JavaScript函数中使用HTML代码
<head>
<script>
function createRow() {
var f = document.createElement("form");
f.setAttribute('method', "post");
f.setAttribute('action', "submit.php");
var i = document.createElement("input"); //input element, text
i.setAttribute('type', "text");
i.setAttribute('name', "username");
var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type', "submit");
s.setAttribute('value', "Submit");
f.appendChild(i);
f.appendChild(s);
//and some more input elements here
//and dont forget to add a submit button
document.getElementsByTagName('body')[0].appendChild(f);
var table1 = document.getElementById("table1");
var newRow = table1.insertRow(3);
var newCell = newRow.insertCell(0);
newCell.innerHTML = "Fe";
newCell = newRow.insertCell(1);
newCell.innerHTML = "Fi";
newCell = newRow.insertCell(2);
newCell.innerHTML = "Fo";
newCell = newRow.insertCell(3);
newCell.innerHTML = "Fum";
document.write("<h1>Out with the old - in with the new!</h1>");
}
</script>
</head>
<body>
<center>
<h1>
Editing HTML Tables
</h1>
<table id="table1" border="2">
<tr>
<td>Fe</td>
<td>Fi</td>
<td>Fo</td>
<td>Fum</td>
</tr>
<tr>
<td>Fe</td>
<td>Fi</td>
<td>Fo</td>
<td>Fum</td>
</tr>
<tr>
<td>Fe</td>
<td>Fi</td>
<td>Fo</td>
<td>Fum</td>
</tr>
</table>
<br>
<input type="button" value="Add a new row" onclick="createRow()">
</center>
</body>
任何人都可以请告诉我如何使用html代码,而不是在我的Javascript功能? 非常感谢。
如果我没看错你想点击“添加新行”按钮后,创建一个新的行? –
预期结果是什么? – guest271314
谢谢你的回答。 Nop,我已经尝试创建一个新的行,我想要的是将JavaScript代码放在JavaScript代码中,以便在同一页中单击显示表单。 –