为什么可以';t我使用JavaScript和a<;部门>;?
由于某些原因,下面的代码无法将额外的表插入到我的为什么可以';t我使用JavaScript和a<;部门>;?,javascript,html,function,button,Javascript,Html,Function,Button,由于某些原因,下面的代码无法将额外的表插入到我的html文档中。文本只是随机地放在表格的顶部,而不是放在表格中。你知道为什么它不起作用吗 <!DOCTYPE html> <head> <script type = "text/javascript"> function insertTable() { var table = document.getElementById("houseListingTable").innerHTML;
html
文档中。文本只是随机地放在表格的顶部,而不是放在表格中。你知道为什么它不起作用吗
<!DOCTYPE html>
<head>
<script type = "text/javascript">
function insertTable() {
var table = document.getElementById("houseListingTable").innerHTML;
table = table + "<tr><td>58,500</td><td>Montreal</td></tr>";
}
</script>
</head>
<body>
<table>
<tr>
<th>Price</th>
<th>Location</th>
</tr>
<div id = "houseListingTable">
</div>
</table>
<button onclick = "insertTable()">Insert Table<\button>
</body>
</html>
函数insertTable(){
var table=document.getElementById(“houseListingTable”).innerHTML;
表=表+58500MONTREAL;
}
价格
位置
插入表格
为什么当我点击
Insert table
按钮时,表行没有添加到我的表中?感谢您的帮助 这里有两个问题:
- 将
元素直接放在
中是无效的HTML - 这里的
变量只是一个字符串。覆盖它对DOM没有影响表
并给
一个ID:
价格
位置
var table=document.getElementById(“houseListingTable”);
table.innerHTML+=“58500montreal”;
请注意,我实际上是如何覆盖表的
.innerHTML
属性的。这是一个重要的区别。这里有两个问题:
- 将
元素直接放在
中是无效的HTML - 这里的
变量只是一个字符串。覆盖它对DOM没有影响表
并给
一个ID:
价格
位置
var table=document.getElementById(“houseListingTable”);
table.innerHTML+=“58500montreal”;
请注意,我实际上是如何覆盖表的
.innerHTML
属性的。这与你的答案有着重要的区别。在回答之前,只需几句评论。请注意,您在begging中缺少html标记,并且您使用了不正确的栏“\”来关闭表标题(应该是“)和按钮标记()中的标记
此外,表中的div不正确
代码什么也不做,因为函数只获取innerHTML。出于您的目的,函数应该获取表中的内容,添加一行,然后将其粘贴回表中
<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript">
function insertTable() {
var table = document.getElementById("houseListingTable").innerHTML;
table = table + "<tr><td>58,500</td><td>Montreal</td></tr>";
document.getElementById("houseListingTable").innerHTML = table;
}
</script>
</head>
<body>
<table id="houseListingTable">
<tr>
<th>Price</th>
<th>Location</th>
</tr>
</table>
<button onclick = "insertTable()">Insert Table</button>
</body>
</html>
函数insertTable(){
var table=document.getElementById(“houseListingTable”).innerHTML;
表=表+58500MONTREAL;
document.getElementById(“houseListingTable”).innerHTML=表格;
}
价格
位置
插入表格
在回答之前,只需几句评论。请注意,您在begging中缺少html标记,并且您使用了不正确的栏“\”来关闭表标题(应该是“)和按钮标记()中的标记
此外,表中的div不正确
代码什么也不做,因为函数只获取innerHTML。出于您的目的,函数应该获取表中的内容,添加一行,然后将其粘贴回表中
<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript">
function insertTable() {
var table = document.getElementById("houseListingTable").innerHTML;
table = table + "<tr><td>58,500</td><td>Montreal</td></tr>";
document.getElementById("houseListingTable").innerHTML = table;
}
</script>
</head>
<body>
<table id="houseListingTable">
<tr>
<th>Price</th>
<th>Location</th>
</tr>
</table>
<button onclick = "insertTable()">Insert Table</button>
</body>
</html>
函数insertTable(){
var table=document.getElementById(“houseListingTable”).innerHTML;
表=表+58500MONTREAL;
document.getElementById(“houseListingTable”).innerHTML=表格;
}
价格
位置
插入表格
您的HTML已损坏。结尾标签是错误的
<table id="houseListingTable">
<tr>
<th>Price</th>
<th>Location</th>
</tr>
</table>
价格
位置
您可以使用DOM方法的insertRow通过首先按Id获取表来向表中添加行
function insertTable() {
// Find a <table> element with id="houseListingTable":
var table = document.getElementById("houseListingTable");
// Create an empty <tr> element and add it to the table:
var row = table.insertRow(table.rows.length);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
// Append a text node to the cell1
var price = document.createTextNode('58,500')
cell1.appendChild(price);
// Append a text node to the cell2
var location = document.createTextNode('Montreal')
cell2.appendChild(location);
}
函数插入表(){
//查找id为=“houseListingTable”的元素:
var table=document.getElementById(“houseListingTable”);
//创建一个空元素并将其添加到表中:
var row=table.insertRow(table.rows.length);
//在“新”元素的第一和第二位置插入新单元格(元素):
var cell1=行插入单元格(0);
var cell2=行插入单元格(1);
//将文本节点追加到单元格1
var price=document.createTextNode('58500')
单元格1.附加子项(价格);
//将文本节点追加到单元格2
var location=document.createTextNode('Montreal')
单元格2.子对象(位置);
}
您的HTML已损坏。结尾标签是错误的
<table id="houseListingTable">
<tr>
<th>Price</th>
<th>Location</th>
</tr>
</table>
价格
位置
您可以使用DOM方法的insertRow通过首先按Id获取表来向表中添加行
function insertTable() {
// Find a <table> element with id="houseListingTable":
var table = document.getElementById("houseListingTable");
// Create an empty <tr> element and add it to the table:
var row = table.insertRow(table.rows.length);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
// Append a text node to the cell1
var price = document.createTextNode('58,500')
cell1.appendChild(price);
// Append a text node to the cell2
var location = document.createTextNode('Montreal')
cell2.appendChild(location);
}
函数插入表(){
//查找id为=“houseListingTable”的元素:
var table=document.getElementById(“houseListingTable”);
//创建一个空元素并将其添加到表中:
var row=table.insertRow(table.rows.length);
//在“新”元素的第一和第二位置插入新单元格(元素):
var cell1=行插入单元格(0);
var cell2=行插入单元格(1);
//将文本节点追加到单元格1
var price=document.createTextNode('58500')
单元格1.附加子项(价格);
//将文本节点追加到单元格2
var location=document.createTextNode('Montreal')
单元格2.子对象(位置);
}
这是无效的html,div不应该是表标记的直接子级,tr不应该是div的直接子级tags@PatrickEvans这就是它不起作用的原因吗?还有其他方法吗?那是无效的html,div不应该是表标记的直接子级,tr不应该是div的直接子级tags@PatrickEvans这就是它不起作用的原因吗?有吗