<script> var rows = document.getElementsByTagName("tr"); for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (row.cells[2].textContent === {{tuihuodanhao}}) { row.classList.add("selected"); } } </script>
时间: 2024-01-15 15:05:38 浏览: 123
在这段代码中,有一处错误:`{{tuihuodanhao}}` 缺少了引号。因为 `textContent` 属性返回的是一个字符串,所以需要将 `{{tuihuodanhao}}` 包裹在引号中,例如:
```
if (row.cells[2].textContent === "{{tuihuodanhao}}") {
row.classList.add("selected");
}
```
这样就能够正确地判断退货单号是否匹配了。同时,根据你的代码,这段 JavaScript 代码应该放在 HTML 文档中的 `<script>` 标签中。
相关问题
<script> var rows = document.getElementsByTagName("tr"); for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (row.cells[0].textContent === '{{id}}') { row.classList.add("selected"); } } </script>
这段 JavaScript 代码获取了所有的 `<tr>` 元素,然后遍历每一行数据。在遍历过程中,它检查第一列的文本内容是否与 "{{id}}" 相同。如果相同,则将该行添加一个名为 "selected" 的 CSS 类,以便可以通过 CSS 样式表来突出显示该行数据。
这段代码通常用于处理表格数据的交互效果,例如当用户选择某一行数据时,可以通过添加 "selected" 类来突出显示该行数据。它使用了 DOM API 中的 `getElementsByTagName` 和 `classList` 方法来获取和修改 HTML 元素的属性和样式。
<script> var rows = document.getElementsByTagName("tr"); var tuihuodanhao = '{{tuihuodanhao}}'; for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (row.cells[2].textContent === tuihuodanhao) { row.classList.add("selected"); } } </script>为什么无效
这段代码无效可能是由于以下原因之一:
1. 变量 `{{tuihuodanhao}}` 没有被正确地设置或传递到了模板中。请确保该变量已正确设置或传递。
2. 在使用 `textContent` 属性获取单元格中的文本内容时,第三个单元格可能不是您想要的列。请确认要查找的列是第三个单元格(即 `row.cells[2]`),或者更改索引以匹配要查找的列。
3. 您的 HTML 中可能没有正确的表格结构,导致无法正确地获取行和单元格。请确保您的表格结构正确,并且每个单元格都正确地包含在行中。
如果您能提供更多上下文或错误信息,我可以更好地理解问题并提供更具体的解决方案。
阅读全文
相关推荐




<script> function goToLink(link) { window.location = link; } </script> <script> var rows = document.getElementsByTagName("tr"); var tuihuodanhao = '{{tuihuodanhao}}'; for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (row.cells[2].textContent === tuihuodanhao) { row.classList.add("selected"); } } </script> <input type="checkbox" id="select-all"> 录入时间 退货单号 录入人 备注 状态 日志 {%for item in data%} <input type="checkbox" value="{{item[1]}}" name="id"> 为什么选中的行不变色
<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>为什么行背景没有变色
</style>
</head>
<body>
<button onclick="addRow()">新增一行</button>
<button onclick="addColumn()">添加一列</button>
<button onclick="deleteRow()">删除行</button>
<button onclick="deleteColumn()">删除列</button>
<button onclick="changeSize()">调整大小</button>
<input type="text" oninput="filterTable(0)" placeholder="工艺膜层">
var table = document.getElementById("myTable");
var imageColumnIndices = [8, 9, 10,11,12,13]; // 指定要添加图片按钮的列索引数组
function addRow() {
var newRow = table.insertRow();
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < cellsCount; i++) {
var newCell = newRow.insertCell();
newCell.contentEditable = true;
if (imageColumnIndices.includes(i)) {
var newInput = document.createElement("input");
newInput.type = "file";
newInput.onchange = function() {
previewImage(this);
};
newInput.onclick = function() {
showPopup(this.src);
};
newCell.appendChild(newInput);
// 点击事件监听器,点击图片按钮时弹出放大图片的弹出窗口
newInput.onclick = function () {
showPopup(this.src);
};
}else {
newCell.innerHTML = "新单元格"; // 将空余按钮的单元格内容设置为默认值
}
}
}
function addColumn() {
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < table.rows.length; i++) {
var newCell = table.rows[i].insertCell();
newCell.contentEditable = true;
newCell.innerHTML = "新单元格";
}
}我需要指定某一列的列宽需要怎么设置
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>鉴本</title>
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1.5px solid black;
padding: 5px;
}
table img {
max-width: 150px;
height: auto;
}
.popup {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
display: flex;
align-items: center;
justify-content: center;
}
.popup img {
max-width: 100%;
max-height: 100%;
cursor: zoom-out;
}
</style>
</head>
<body>
<button onclick="addRow()">新增一行</button>
<button onclick="addColumn()">添加一列</button>
<button onclick="deleteRow()">删除行</button>
<button onclick="deleteColumn()">删除列</button>
<button onclick="changeSize()">调整大小</button>
var table = document.getElementById("myTable");
var imageColumnIndices = [8, 9, 10,11,12,13]; // 指定要添加图片按钮的列索引数组
function addRow() {
var newRow = table.insertRow();
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < cellsCount; i++) {
var newCell = newRow.insertCell();
newCell.contentEditable = true;
if (imageColumnIndices.includes(i)) {
var newInput = document.createElement("input");
newInput.type = "file";
newInput.onchange = function() {
previewImage(this);
};
newInput.onclick = function() {
showPopup(this.src);
};
newCell.appendChild(newInput);
// 点击事件监听器,点击图片按钮时弹出放大图片的弹出窗口
newInput.onclick = function () {
showPopup(this.src);
};
}else {
newCell.innerHTML = "新单元格"; // 将空余按钮的单元格内容设置为默认值
}
}
}
function addColumn() {
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < table.rows.length; i++) {
var newCell = table.rows[i].insertCell();
newCell.contentEditable = true;
newCell.innerHTML = "新单元格";
}
}

<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>为什么行背景没有变色



</style>
</head>
<body>
<button onclick="addRow()">新增一行</button>
<button onclick="addColumn()">添加一列</button>
<button onclick="deleteRow()">删除行</button>
<button onclick="deleteColumn()">删除列</button>
<button onclick="changeSize()">调整大小</button>
<input type="text" oninput="filterTable(0)" placeholder="工艺膜层">
var table = document.getElementById("myTable");
var imageColumnIndices = [8, 9, 10,11,12,13]; // 指定要添加图片按钮的列索引数组
function addRow() {
var newRow = table.insertRow();
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < cellsCount; i++) {
var newCell = newRow.insertCell();
newCell.contentEditable = true;
if (imageColumnIndices.includes(i)) {
var newInput = document.createElement("input");
newInput.type = "file";
newInput.onchange = function() {
previewImage(this);
};
newInput.onclick = function() {
showPopup(this.src);
};
newCell.appendChild(newInput);
// 点击事件监听器,点击图片按钮时弹出放大图片的弹出窗口
newInput.onclick = function () {
showPopup(this.src);
};
}else {
newCell.innerHTML = "新单元格"; // 将空余按钮的单元格内容设置为默认值
}
}
}
function addColumn() {
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < table.rows.length; i++) {
var newCell = table.rows[i].insertCell();
newCell.contentEditable = true;
newCell.innerHTML = "新单元格";
}
}我需要指定某一列的列宽需要怎么设置
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>鉴本</title>
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1.5px solid black;
padding: 5px;
}
table img {
max-width: 150px;
height: auto;
}
.popup {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
display: flex;
align-items: center;
justify-content: center;
}
.popup img {
max-width: 100%;
max-height: 100%;
cursor: zoom-out;
}
</style>
</head>
<body>
<button onclick="addRow()">新增一行</button>
<button onclick="addColumn()">添加一列</button>
<button onclick="deleteRow()">删除行</button>
<button onclick="deleteColumn()">删除列</button>
<button onclick="changeSize()">调整大小</button>
var table = document.getElementById("myTable");
var imageColumnIndices = [8, 9, 10,11,12,13]; // 指定要添加图片按钮的列索引数组
function addRow() {
var newRow = table.insertRow();
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < cellsCount; i++) {
var newCell = newRow.insertCell();
newCell.contentEditable = true;
if (imageColumnIndices.includes(i)) {
var newInput = document.createElement("input");
newInput.type = "file";
newInput.onchange = function() {
previewImage(this);
};
newInput.onclick = function() {
showPopup(this.src);
};
newCell.appendChild(newInput);
// 点击事件监听器,点击图片按钮时弹出放大图片的弹出窗口
newInput.onclick = function () {
showPopup(this.src);
};
}else {
newCell.innerHTML = "新单元格"; // 将空余按钮的单元格内容设置为默认值
}
}
}
function addColumn() {
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < table.rows.length; i++) {
var newCell = table.rows[i].insertCell();
newCell.contentEditable = true;
newCell.innerHTML = "新单元格";
}
}

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>鉴本</title>
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1.5px solid black;
padding: 5px;
}
table img {
max-width: 150px;
height: auto;
}
.popup {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
display: flex;
align-items: center;
justify-content: center;
}
.popup img {
max-width: 100%;
max-height: 100%;
cursor: zoom-out;
}
</style>
</head>
<body>
<button onclick="addRow()">新增一行</button>
<button onclick="addColumn()">添加一列</button>
<button onclick="deleteRow()">删除行</button>
<button onclick="deleteColumn()">删除列</button>
<button onclick="changeSize()">调整大小</button>
var table = document.getElementById("myTable");
var imageColumnIndices = [8, 9, 10,11,12,13]; // 指定要添加图片按钮的列索引数组
function addRow() {
var newRow = table.insertRow();
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < cellsCount; i++) {
var newCell = newRow.insertCell();
newCell.contentEditable = true;
if (imageColumnIndices.includes(i)) {
var newInput = document.createElement("input");
newInput.type = "file";
newInput.onchange = function() {
previewImage(this);
};
newInput.onclick = function() {
showPopup(this.src);
};
newCell.appendChild(newInput);
// 点击事件监听器,点击图片按钮时弹出放大图片的弹出窗口
newInput.onclick = function () {
showPopup(this.src);
};
}else {
newCell.innerHTML = "新单元格"; // 将空余按钮的单元格内容设置为默认值
}
}
}
function addColumn() {
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < table.rows.length; i++) {
var newCell = table.rows[i].insertCell();
newCell.contentEditable = true;
newCell.innerHTML = "新单元格";
}
}








