Javascript:更改表行 HTML



我正在为我们公司的报价软件创建一个模板,该软件会生成一个 HTML 页面并动态填充一个表格,每个引用的项目都是一个新行。

我需要检查报价中的特定零件号,如果存在,则需要更改该行的 HTML。

这是 HTML 的一个片段

<div id="line-items-section" class="section">
<table id="line-item-detail-table" class="table-001">
<tr>
<!-- <th>
</th> -->
<th>
QTY
</th>
<th width="70%">
Item
</th>
<th>
Unit Price
</th>
<th>
Ext Price
</th>
</tr>
[Begin_LineItem] [Begin_LineTypeProduct]
<tr id="canChange" class="row-product">
<!-- <td class="col-option-box">
[QV_OptionBox]
</td> -->
<td class="col-qty">
[DI_QtyBase]
</td>
<td class="col-partnumber">
[DI_ManufacturerPartNumber]
</td>
<td class="col-unit-price">
[DI_UnitPrice:f=1]
</td>
<td class="col-extended-price">
[DI_ExtendedPrice:f=1]
</td>
</tr>
<tr class="row-product-description">
<!-- <td class="col-option-box">
[QV_OptionBox]
</td> -->
<td class="col-qty">
</td>
<td colspan="3" class="col-description">
[DI_Description]
</td>
</tr>
[End_LineTypeProduct]

然后我有这个Javascript文件:

var matches = document.querySelectorAll("td.col-partnumber");
for(var value of matches.values()) {
//console.log(value.innerText);
if (value.innerText == "SLAB KIT FORM") {
var str = '<tr class="row-comment"><td class="col-qty"></td><td colspan="4" class="col-description">[DI_Description]</td></tr>';
var Obj = document.getElementById('canChange');
Obj.outerHTML = str;
}
}

我正在使用querySelectorAll和For循环来查找我想要的零件号(我正在寻找"板坯套件形式"(。 然后使用 outerHTML 来更改 HTML。

这部分有效。

结果是,找到了部件号,但代码只更改了表中的第一行...我正在寻找的表格行可以出现在表格中的任何位置(甚至在同一表中多次出现(。

从本质上讲,我只需要让此代码针对包含具有 SLAB 套件表单的单元格的表行

不应向每一行添加相同的 id。通过 javascript 找到正确的表单元格后,您可以通过parentNode属性找到相应的行。

例如像这样:

window.addEventListener('load', function() {
const button = document.querySelector('.run-script')
button.addEventListener('click', function() {
var matches = document.querySelectorAll("td.col-partnumber");
for (var value of matches.values()) {
// console.info(value);
if (value.innerText == "SLAB KIT FORM") {
var str = '<tr class="row-comment"><td class="col-qty"></td><td colspan="4" class="col-description">[DI_Description]</td></tr>';
var Obj = value.parentNode;
Obj.outerHTML = str;
}
}
})
})
button {
padding: 1rem;
background-color: darkred;
color: white;
margin-bottom: 1rem;
width: 25%;
min-width: 200px;
font-size: 1.2rem;
text-transform: uppercase;
cursor: pointer;
}
table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
table td, #customers th {
border: 1px solid #ddd;
padding: 8px;
}
table tr:nth-child(even){background-color: #f2f2f2;}
table tr:hover {background-color: #ddd;}
table th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
<button class="run-script" type="button">Run Script</button>
<div id="line-items-section" class="section">
<table id="line-item-detail-table" class="table-001">
<tr>
<!-- <th>
</th> -->
<th>
QTY
</th>
<th width="70%">
Item
</th>
<th>
Unit Price
</th>
<th>
Ext Price
</th>
</tr>
<tr class="row-product">
<td class="col-qty">
5
</td>
<td class="col-partnumber">
NOT THE PARTNUMBER YOU'RE LOOKING FOR
</td>
<td class="col-unit-price">
40
</td>
<td class="col-extended-price">
forty
</td>
</tr>
<tr class="row-product-description">
<td class="col-qty">
</td>
<td colspan="3" class="col-description">
some description
</td>
</tr>
<tr class="row-product">
<td class="col-qty">
8
</td>
<td class="col-partnumber">
SLAB KIT FORM
</td>
<td class="col-unit-price">
20
</td>
<td class="col-extended-price">
twenty
</td>
</tr>
<tr class="row-product-description">
<td class="col-qty">
</td>
<td colspan="3" class="col-description">
other description
</td>
</tr>
</table>
</div>

最新更新