简体   繁体   English

Javascript 更改内部 HTML

[英]Javascript change inner HTML

I'd like to change the value of a label depending on the status of its checkbox我想根据复选框的状态更改标签的值

 function private() { var checkBox = document.getElementById("private");// Get the checkbox var text = document.querySelector('label[for="private"]');// Get the output text if (checkBox.checked == true) { text.innerHTML = "Public"; } else { text.innerHTML = "Private"; } }
 <label class="switch"> <input id="private" type="checkbox" onclick="private()" /> <span class="slider"></span> </label> <label for="private"></label>

Why doesn't this work?为什么这不起作用?

querySelectorAll returns a nodelist, so you need to specify the element you want like: querySelectorAll返回一个节点列表,所以你需要指定你想要的元素:

 function private() { var checkBox = document.getElementById("private");// Get the checkbox var text = document.querySelectorAll('label[for="private"]')[0];// Get the output text if (checkBox.checked == true) { text.innerHTML = "Public"; } else { text.innerHTML = "Private"; } }
 <label class="switch"> <input id="private" type="checkbox" onclick="private()" /> <span class="slider"></span> </label> <label for="private"></label>

Or maybe just use querySelector instead which only returns the first match:或者也许只是使用querySelector而不是只返回第一个匹配项:

 function private() { var checkBox = document.getElementById("private");// Get the checkbox var text = document.querySelector('label[for="private"]');// Get the output text if (checkBox.checked == true) { text.innerHTML = "Public"; } else { text.innerHTML = "Private"; } }
 <label class="switch"> <input id="private" type="checkbox" onclick="private()" /> <span class="slider"></span> </label> <label for="private"></label>

You need to use querySelector() not querySelectorAll() .您需要使用querySelector()而不是querySelectorAll() By getting all elements, you would have to iterate through the list of items (even though there is only 1).通过获取所有元素,您必须遍历项目列表(即使只有 1 个)。

 function private() { var checkBox = document.getElementById("private");// Get the checkbox var text = document.querySelector('label[for="private"]');// Get the output text if (checkBox.checked == true) { text.innerHTML = "Public"; } else { text.innerHTML = "Private"; } }
 <label class="switch"> <input id="private" type="checkbox" onclick="private()" /> <span class="slider"></span> </label> <label for="private"></label>

There are three things that I would suggest that will solve your problem:我建议三件事可以解决您的问题:

  • Pass the event object to your function.event对象传递给您的函数。 This contains everything about the click event, including what element was clicked on.这包含有关click事件的所有内容,包括click元素。 This means you don't have to scour the DOM.这意味着您不必搜索 DOM。
  • Do not wrap a form control with a label .不要用包装标签的形式控制。 This is not how labels were intended to work.这不是标签的工作方式。 Instead use something like a section tag.而是使用类似section标签的东西。
  • Form controls and their labels are automatically connected when a for attribute is provided.当提供for属性时,表单控件及其标签会自动连接。 The other can be found through control.labels[0] or label.htmlFor .另一个可以通过control.labels[0]label.htmlFor This is particularly why you want to adhere to the standard of not wrapping controls with labels这就是为什么您要遵守不使用标签包装控件的标准的原因

With these adjustments, and the addition of a ternary condition statement to determine what text to display, the code will look like this:通过这些调整,并添加一个三元条件语句来确定要显示的文本,代码将如下所示:

<section class="switch">
  <input id="private" type="checkbox" onclick="private(event)" />
  <span class="slider"></span>
</section>

<label for="private"></label>

function private(e) {
  var checkBox = e.currentTarget,
  label = checkBox.labels[0];

  label.textContent = checkBox.checked ? "Public" : "Private";
}

Example:例子:

 function private(e) { var checkBox = e.currentTarget, label = checkBox.labels[0]; label.textContent = checkBox.checked ? "Public" : "Private"; }
 <section class="switch"> <input id="private" type="checkbox" onclick="private(event)" /> <span class="slider"></span> </section> <label for="private"></label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM