请帮助我:)
我有这个 HTML:
<div class="delivery-time">Delivery to: <strong>18.7.2018</strong></div>
如何更改 Javascript 以获得此 HTML?:
<div class="delivery-time">Ship to: <strong>18.7.2018</strong></div>
这意味着,<strong>
中有相同的日期,但更改 <div>
中的文本(之前<strong>
)
谢谢。
最佳答案
您需要定位第一个 text node在元素中使用 firstChild 。您可以仅更改其 textContent并保持元素内的其他节点不变:
document.querySelector('.delivery-time').firstChild.textContent = 'Ship to: ';
<div class="delivery-time">Delivery to: <strong>18.7.2018</strong></div>
关于javascript - 如何通过javascript更改div中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51370362/