var select = row.getElementById("包区");
时间: 2023-06-18 09:03:24 浏览: 48
这段代码应该出错了。`row` 是一个 HTML 表格中的行元素,但是行元素并没有 `getElementById` 方法。如果你想获取该行中 ID 为 `"包区"` 的元素,可以使用以下代码:
```javascript
var select = row.querySelector("#包区");
```
或者,如果你确定该行中只有一个 ID 为 `"包区"` 的元素,也可以直接使用以下代码:
```javascript
var select = row.getElementById("包区");
```
但是这种方法在标准 HTML 中是错误的,因为 ID 必须在整个文档中是唯一的,不应该在同一个文档中出现多次。
相关问题
var row = selectId.parentNode.parentNode; var select = row.getElementById(selectId);
这段代码有错误。在JavaScript中,节点对象没有 `getElementById` 方法,只有 `document` 对象有这个方法。如果你想选择一个节点下的某个元素,可以使用 `querySelector` 或者 `querySelectorAll` 方法。
以下是修改后的代码:
```
var row = selectId.parentNode.parentNode;
var select = row.querySelector('#' + selectId);
```
html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Dynamic HTML Table</title> <style> table, th, td { border: 1px solid black; border-collapse: collapse; padding: 5px; } </style> </head> <body> <button onclick="addRow()">Add Row</button> <table id="myTable"> <thead> <tr> <th th:text="#{column1}"></th> <th th:text="#{column2}"></th> <th th:text="#{column3}"></th> <th th:text="#{column4}"></th> <th th:text="#{column5}"></th> </tr> </thead> <tbody> <tr th:each="row, rowStat : ${rows}"> <td th:each="cell, cellStat : ${row}" th:onclick="'selectOption(this, ' + ${rowStat.index} + ', ' + ${cellStat.index} + ')'" th:text="${cell}"></td> </tr> </tbody> </table> <script> var rows = [[null, null, null, null, null]]; function addRow() { rows.push([null, null, null, null, null]); renderTable(); } function selectOption(cell, rowIndex, cellIndex) { var options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]; var select = document.createElement("select"); for (var i = 0; i < options.length; i++) { var option = document.createElement("option"); option.value = options[i]; option.text = options[i]; select.appendChild(option); } cell.innerHTML = ""; cell.appendChild(select); rows[rowIndex][cellIndex] = select; } function renderTable() { var table = document.getElementById("myTable").getElementsByTagName('tbody')[0]; table.innerHTML = ""; for (var i = 0; i < rows.length; i++) { var row = table.insertRow(-1); for (var j = 0; j < rows[i].length; j++) { var cell = row.insertCell(j); if (rows[i][j] != null) { cell.appendChild(rows[i][j]); } else { cell.onclick = function() { selectOption(this, i, j) }; cell.innerHTML = "Click to Select"; } } } } renderTable(); </script> </body> </html>把这个页面用bootstrap进行美化一下
动态HTML表格">Dynamic HTML Table</th> </tr> </thead> <tbody> </tbody> </table> <script> function addRow() { var table = document.getElementById("myTable"); var row = table.insertRow(-1); var cell1 = row.insertCell(0); cell1.innerHTML = "New Row"; } </script> </body> </html>
这段代码是一个HTML页面,其中包含了一个动态的HTML表格。通过点击一个按钮,可以动态地在表格中添加一行。表格中包含一个表头和一个tbody,表头中只有一列。整个页面使用thymeleaf作为模板引擎,样式使用CSS定义。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<script> function goToLink(link) { window.location = link; } </script> <input type="checkbox" id="select-all"> 录入时间 退货单号 录入人 备注 状态 日志 {%for item in data%} <input type="checkbox" value="{{item[1]}}" name="id"> {{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {% if item[4] is none %}{% else %}{{item[4]}}{% endif %} 日志 {%endfor%} <script> var rows = document.getElementsByTagName("tr"); for (var i = 0; i < rows.length; i++) { var row = rows[i]; var tuihuodanhao = row.cells[1].innerHTML; if (tuihuodanhao === {{tuihuodanhao}}) { row.classList.add("selected"); } } </script> <script> function fun(ido,page){ var textDiv= document.getElementById(ido); var url="{{ url_for('shouhou.show_tui') }}?page="+page+"&keyword={% if keyword!=None %}{{keyword}}{% endif %}"; return textDiv.setAttribute("href",url); } </script>为什么行背景没有变色
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)