简体   繁体   English

隐藏/显示div复选框,单击

[英]hide / show div on checkbox click

hello i was just trying to hide / show div's on checkbox click i have tried the code / script below but it didn't work ? 你好,我只是想隐藏/显示div的复选框,我尝试了下面的代码/脚本,但是没有用? i was also wondering if there is a easier way to do it? 我也想知道是否有更简单的方法?

 function checkBox1() { var checkBox = document.getElementById("myCheck1"); var text = document.getElementsByClassName("shop"); if (checkBox.checked == true){ text.style.display = "block"; } else { text.style.display = "none"; } } function checkBox2() { var checkBox = document.getElementById("myCheck2"); var text = document.getElementsByClassName("party"); if (checkBox.checked == true){ text.style.display = "block"; } else { text.style.display = "none"; } } function checkBox3() { var checkBox = document.getElementById("myCheck3"); var text = document.getElementById("dailyquests"); if (checkBox.checked == true){ text.style.display = "block"; } else { text.style.display = "none"; } } 
 <center>| Shop: <input type="checkbox" id="myCheck1" onclick="checkBox1()" checked> | Party: <input type="checkbox" id="myCheck2" onclick="checkBox2()" checked> | Dailyquests: <input type="checkbox" id="myCheck3" onclick="checkBox3()" checked> |</center> <br> <br> <div class="shop">TEXT HERE</div> <br> <div class="party">TEXT HERE</div> <br> <div class="dailyquests">TEXT HERE</div> 

sorry if my english is not good 对不起,如果我的英语不好

document.getElementsByClassName return a list of elements. document.getElementsByClassName返回元素列表。 the text object is in the first element : 文本对象在第一个元素中:

 function checkBox1() { var checkBox = document.getElementById("myCheck1"); var text = document.getElementsByClassName("shop")[0]; if (checkBox.checked == true){ text.style.display = "block"; } else { text.style.display = "none"; } } function checkBox2() { var checkBox = document.getElementById("myCheck2"); var text = document.getElementsByClassName("party")[0]; if (checkBox.checked == true){ text.style.display = "block"; } else { text.style.display = "none"; } } function checkBox3() { var checkBox = document.getElementById("myCheck3"); var text = document.getElementsByClassName("dailyquests")[0]; if (checkBox.checked == true){ text.style.display = "block"; } else { text.style.display = "none"; } } 
 <center>| Shop: <input type="checkbox" id="myCheck1" onclick="checkBox1()" checked> | Party: <input type="checkbox" id="myCheck2" onclick="checkBox2()" checked> | Dailyquests: <input type="checkbox" id="myCheck3" onclick="checkBox3()" checked> |</center> <br> <br> <div class="shop">TEXT HERE</div> <br> <div class="party">TEXT HERE</div> <br> <div class="dailyquests">TEXT HERE</div> 

Document.getElementsByClassName() returns an array-like object of all child elements which have all of the given class names. Document.getElementsByClassName()返回具有所有给定类名称的所有子元素的类似数组的对象。

change this: 改变这个:

document.getElementsByClassName("shop");
document.getElementsByClassName("party");
document.getElementsByClassName("dailyquests");

to: 至:

document.getElementsByClassName("shop")[0];
document.getElementsByClassName("party")[0];
document.getElementsByClassName("dailyquests")[0];

to take the first element with that class 在该课程中采用第一个要素

Ps. PS。

In the third function you have to use 'document.getElementsByClassName' and not 'document.getElementById', because 'dailyquests' is a class, not an id 在第三个函数中,您必须使用“ document.getElementsByClassName”而不是“ document.getElementById”,因为“ dailyquests”是一个类,而不是一个id

Demo : jsfiddle 演示jsfiddle

This is because document.getElementsByClassName returns a collection & you need to pass the index to get the element. 这是因为document.getElementsByClassName返回一个集合,并且您需要传递索引才能获取该元素。

Having said that you can modify the javascript and have only one function to do the work of displaying and hiding. 话虽如此,您可以修改javascript并只有一个功能来完成显示和隐藏的工作。

For example <input type="checkbox" id="myCheck1" onclick="toggleDisplay(this.id,'shop')" checked on click will call a toggleDisplay function and the id is passed using this.id . 例如,单击时<input type="checkbox" id="myCheck1" onclick="toggleDisplay(this.id,'shop')" checked将调用toggleDisplay函数,并且使用this.id传递this.id In toggleDisplay(this.id,'shop')` the second parameter is the class name In toggleDisplay(this.id,'shop')`中,第二个参数是类名

 function toggleDisplay(elemId, className) { if (document.getElementById(elemId).checked) { document.getElementsByClassName(className)[0].style.display = "block" } else { document.getElementsByClassName(className)[0].style.display = "none" } } 
 <center>| Shop: <input type="checkbox" id="myCheck1" onclick="toggleDisplay(this.id,'shop')" checked> | Party: <input type="checkbox" id="myCheck2" onclick="toggleDisplay(this.id,'party')" checked> | Dailyquests: <input type="checkbox" id="myCheck3" onclick="toggleDisplay(this.id,'dailyquests')" checked> | </center> <br> <br> <div class="shop">TEXT HERE</div> <br> <div class="party">TEXT HERE</div> <br> <div class="dailyquests">TEXT HERE</div> 

So getElementsByClassName returns a collection of items, you would need to choose the index - 因此, getElementsByClassName返回项目的集合,您需要选择索引-

function checkBox1() {    
  var checkBox = document.getElementById("myCheck1");
  var text = document.getElementsByClassName("shop");
  if (checkBox.checked){
    text[0].style.display = "block";
  } else {
   text[0].style.display = "none";
  }
}

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

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