我想将
style="font-weight: bold;"
添加到下面 html 的 label 标签中:<label for="Results_CheckZ" style="font-weight: bold;">Sample content</label>
<div id="Results_CheckZField" class="field-normal both checkZ">
<div class="field-control">
<div class="container-normal">
<label for="Results_CheckZ">Sample content</label>
</div>
</div>
</div>
我尝试了以下方法,但没有成功。
当它没有
label
或 id
时,我不知道如何获取 class
标签。const targetLabel = document.querySelector('#Results_CheckZField .field-control .container-normal');
targetLabel.setAttribute("style", "font-weight: bold;");
直接在container-normal类中选择label标签,然后将样式应用到它。
const targetLabel = document.querySelector('#Results_CheckZField .field-control .container-normal label');
targetLabel.style.fontWeight = "bold";
const targetLabel = document.querySelector('#Results_CheckZField .field-control .container-normal label');
targetLabel.style.fontWeight = "bold";
<div id="Results_CheckZField" class="field-normal both checkZ">
<div class="field-control">
<div class="container-normal">
<label for="Results_CheckZ">Sample content</label>
</div>
</div>
</div>