使用 innerText Javascript 更改标签的值

Change the value of an label using innerText Javascript

我正在尝试更改 ul 内 li 内标签的文本值。

所以:

HTML 和 JS

document.getElementsByClassName("hs-error-msgs inputs-list").innerText = "Bitte füllen Sie dieses Pflichtfeld aus!";
<ul class="hs-error-msgs inputs-list" style="display:block;" role="alert" data-reactid=".hbspt-forms-0.0:[=12=].3">
  <li data-reactid=".hbspt-forms-0.0:[=12=].3.[=12=]"> <label data-reactid=".hbspt-forms-0.0:[=12=].3.[=12=].0">Please complete this required field. </label> </li>
</ul>

getElementsByClassName returns a collection. You could take the first element of this collection and then select label within it with for example another collection method getElementsByTagName.

var ul = document.getElementsByClassName("hs-error-msgs inputs-list")[0]
var label = ul.getElementsByTagName('label')[0]

label.innerText = "Bitte füllen Sie dieses Pflichtfeld aus!";
<ul class="hs-error-msgs inputs-list" style="display:block;" role="alert" data-reactid=".hbspt-forms-0.0:[=11=].3">
  <li data-reactid=".hbspt-forms-0.0:[=11=].3.[=11=]"> <label data-reactid=".hbspt-forms-0.0:[=11=].3.[=11=].0">Please complete this required field. </label> </li>
</ul>

然而更方便的方法是 querySelector:

var label = document.querySelector(".hs-error-msgs.inputs-list label")

label.innerText = "Bitte füllen Sie dieses Pflichtfeld aus!";
<ul class="hs-error-msgs inputs-list" style="display:block;" role="alert" data-reactid=".hbspt-forms-0.0:[=11=].3">
  <li data-reactid=".hbspt-forms-0.0:[=11=].3.[=11=]"> <label data-reactid=".hbspt-forms-0.0:[=11=].3.[=11=].0">Please complete this required field. </label> </li>
</ul>

您需要查询label,使用querySelector

var text = "Bitte füllen Sie dieses Pflichtfeld aus!";
document.querySelector( ".hs-error-msgs inputs-list li label").innerText = text;

使用 querySelector 获取一行中的单个项目。

document.querySelector(".hs-error-msgs.inputs-list label").innerText = "Bitte füllen Sie dieses Pflichtfeld aus!";

这将 select 带有您指定 类 的元素内部的单个标签。如果您有多个项目,请使用 querySelectorAll 并使用 for 循环进行迭代。