使用 native JavaScript,我需要将下面的文本从 None
更改为 Black
我知道使用 Jquery 或其他库很容易,但在这个项目中我无法使用它们,有人可以帮助我吗?
<li>
<input type="radio" id="options_82" class="radio product-custom-option" name="options[82]">
<span class="label">
<label for="options_82">None</label>
</span>
</li>
最佳答案
使用 querySelector
获取元素, getElementById
等然后将其设置为 'Black'
使用 textContent
或innerHTML
特性。最佳实践是使用 textContent
除非您实际设置 HTML。
document.querySelector('label[for="options_82"]').textContent = 'Black';
使用 ID 的更复杂的选择器可能比上面的更快:
document.querySelector('#options_82 + .label label').textContent = 'Black';
关于javascript - 使用 native JavaScript 更改子元素的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26023510/