[英]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:我建议三件事可以解决您的问题:
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.section
tag.section
标签的东西。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 labelsWith 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";
}
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.