2 回答

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
試試這個(gè)
如果您只想隱藏該行,您只需要 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>
如果標(biāo)簽和元素在不同的行中,您需要 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貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
也許在加載時(shí)執(zhí)行此操作,那么您稍后需要執(zhí)行的操作會(huì)容易得多。
const child = document.querySelectorAll('label[for="common_id1"]')
child.forEach(node => node.parentNode.parentNode.data('targetId', node.id))
添加回答
舉報(bào)