2 回答
TA贡献1887条经验 获得超5个赞
试试这个
如果您只想隐藏该行,您只需要 ID:
var id = "common_id2";
[...document.querySelectorAll("#"+id)].forEach(function(ele) {
ele.closest("tr").style.display="none";
})
<table>
<tbody>
<tr>
<td>
<div class="csLabel">
<label for="common_id1">Label</label>
</div>
</td>
<td>
<div class="csFields">
<select id="common_id1" name="someName">
<option value="">some options...</option>
</select>
</div>
</td>
</tr>
<tr>
<td>
<div class="csLabel">
<label for="common_id2">Label</label>
</div>
</td>
<td>
<div class="csFields">
<select id="common_id2" name="someName">
<option value="">some options...</option>
</select>
</div>
</td>
</tr>
</tbody>
</table>
如果标签和元素在不同的行中,您需要 ID 和 for=ID
var id = "common_id2";
[...document.querySelectorAll("#" + id + ", [for=" + id + "]")].forEach(function(ele) {
ele.closest("tr").style.display = "none";
})
<table>
<tbody>
<tr>
<td>
<div class="csLabel">
<label for="common_id1">Label</label>
</div>
</td>
</tr>
<tr>
<td>
<div class="csFields">
<select id="common_id1" name="someName">
<option value="">some options...</option>
</select>
</div>
</td>
</tr>
<tr>
<td>
<div class="csLabel">
<label for="common_id2">Label</label>
</div>
</td>
</tr>
<tr>
<td>
<div class="csFields">
<select id="common_id2" name="someName">
<option value="">some options...</option>
</select>
</div>
</td>
</tr>
</tbody>
</table>
TA贡献2016条经验 获得超9个赞
也许在加载时执行此操作,那么您稍后需要执行的操作会容易得多。
const child = document.querySelectorAll('label[for="common_id1"]')
child.forEach(node => node.parentNode.parentNode.data('targetId', node.id))
添加回答
举报