javascript – getElementsByClassName不起作用,但是getElementById呢?

前端之家收集整理的这篇文章主要介绍了javascript – getElementsByClassName不起作用,但是getElementById呢?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

参见英文答案 > getElementsByClassName not working                                     2个
我写了一个脚本,它的目标是停止显示图像一和二,同时允许图像3保持显示并移动到它们的位置.当我使用div Id而不是div Classes时,它工作正常,但我更喜欢使用div类,所以我可以像这样对元素进行分组:

  1. function myFunction() {
  2. var y = document.getElementsByClassName("firstimage secondimage");
  3. if (y.style.display === 'none') {
  4. y.style.display = 'block';
  5. } else {
  6. y.style.display = 'none';
  7. }
  8. }

而不是这个(为了节省空间,我应该选择包含更多元素):

  1. function myFunction() {
  2. var x = document.getElementById("firstimage");
  3. if (x.style.display === 'none') {
  4. x.style.display = 'block';
  5. } else {
  6. x.style.display = 'none';
  7. }
  8. var y = document.getElementById("secondimage");
  9. if (y.style.display === 'none') {
  10. y.style.display = 'block';
  11. } else {
  12. y.style.display = 'none';
  13. }
  14. }

我认为只是将div id改为div类,#imagenumber改为.imagenumber(除了我上面描述的javascript的改变之外)会有效,但是当我这样做时脚本停止工作.我需要脚本以与我在下面粘贴的代码相同的方式运行,但是使用div类而不是div Id.请告诉我哪里出错了.

CSS:

  1. #firstimage {
  2. width: 100px;
  3. height: 100px;
  4. padding: 0px 0;
  5. text-align: center;
  6. background-color: green;
  7. margin-top:20px;
  8. color: white;
  9. }
  10. #secondimage {
  11. width: 100px;
  12. height: 100px;
  13. padding: 0px 0;
  14. text-align: center;
  15. background-color: blue;
  16. margin-top:20px;
  17. color: white;
  18. }
  19. #thirdimage {
  20. width: 100px;
  21. height: 100px;
  22. padding: 0px 0;
  23. text-align: center;
  24. background-color: red;
  25. margin-top:20px;
  26. color: white;
  27. }

HTML:

使用Javascript:

  1. function myFunction() {
  2. var x = document.getElementById("firstimage");
  3. if (x.style.display === 'none') {
  4. x.style.display = 'block';
  5. } else {
  6. x.style.display = 'none';
  7. }
  8. var y = document.getElementById("secondimage");
  9. if (y.style.display === 'none') {
  10. y.style.display = 'block';
  11. } else {
  12. y.style.display = 'none';
  13. }
  14. }
最佳答案
您应该使用getElementsByClassName()或querySelectorAll()来收集所有div.Klass(Klass是一个任意名称).以下代码段使用querySelectorAll()详细信息在源中进行了注释.

SNIPPET

  1. function toggleDiv() {
  2. // Collect all .image into a NodeList
  3. var xs = document.querySelectorAll(".image");
  4. // Declare i and qty for "for" loop
  5. var i,qty = xs.length;
  6. // Use "for" loop to iterate through NodeList
  7. for (i = 0; i < qty; i++) {
  8. // If this div.image at index [i] is "none"...
  9. if (xs[i].style.display === 'none') {
  10. // then make it "block"...
  11. xs[i].style.display = 'block';
  12. } else {
  13. // otherwise set display to "none"
  14. xs[i].style.display = 'none';
  15. }
  16. }
  17. }
  1. #firstimage {
  2. width: 100px;
  3. height: 100px;
  4. padding: 0px 0;
  5. text-align: center;
  6. background-color: green;
  7. margin-top: 20px;
  8. color: white;
  9. }
  10. #secondimage {
  11. width: 100px;
  12. height: 100px;
  13. padding: 0px 0;
  14. text-align: center;
  15. background-color: blue;
  16. margin-top: 20px;
  17. color: white;
  18. }
  19. #thirdimage {
  20. width: 100px;
  21. height: 100px;
  22. padding: 0px 0;
  23. text-align: center;
  24. background-color: red;
  25. margin-top: 20px;
  26. color: white;
  27. }

在这个函数中,只需使用一个“类似数组”的对象,如上面Snippet中演示的NodeList.将以与Snippet中相同的方式使用数组.你是否想要对div进行更高级的处理,例如在每个div上运行一个函数并返回,然后将“类似数组”的对象转换为数组是运行map,forEach,slice等方法所必需的.

猜你在找的HTML相关文章