className only changing every other class

后端 未结 15 2203
陌清茗
陌清茗 2020-12-01 23:41

I\'m performing a small text with JavaScript with the getElementsByClassName() and I am getting some unwanted results. I would like the script to change each CS

15条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 00:16

    Definition and Usage

    1. The getElementsByClassName() method returns a collection of all elements in the document with the specified class name, as a NodeList object.

    2. The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.

    The use of this property is discouraged because of performance implications (due to the live DOMCollection where any changes to the document must be reflected on the returned object immediately) and complexity (the removal of an element from the document will result in immediate changes to the collection).

    And by just adding only blockSet[0].className = "block-selected"; and by clicking on the button it colord each div by each click so we need to click 8 times in order to color all the divs and see the live example below

    function myFunction() {
      var blockSet = document.getElementsByClassName('block-default');
    
      blockSet[0].className = "block-selected";
    
    }
    .block-default {
      width: 100px;
      height: 50px;
      background-color: green;
      border: 1px solid red;
      padding: 10px;
    }
    .block-selected {
      width: 100px;
      height: 50px;
      background-color: blue;
      border: 1px solid white;
      padding: 10px;
    }
    
    
    
    BLOCK1
    BLOCK2
    BLOCK3
    BLOCK4
    BLOCK5
    BLOCK6
    BLOCK7
    BLOCK8

    And by adding only var blockSet = document.getElementsByClassName('block-default'); alert("Length are: " + blockSet.length + "\nFirst Item is: " + blockSet[0].childNodes[0].nodeValue); without the rest it will alert

    • Length are: 8
    • First Item is: block1

    As in the below live example:

    function myFunction() {
      var blockSet = document.getElementsByClassName('block-default');
    
      /*
          blockSet[0].className = "block-selected";
          blockSet[1].className = "block-selected";
          blockSet[2].className = "block-selected";
          blockSet[3].className = "block-selected";
          blockSet[4].className = "block-selected";
          blockSet[5].className = "block-selected";
          blockSet[6].className = "block-selected";
          blockSet[7].className = "block-selected";*/
    
      alert("Length are: " + blockSet.length + "\nFirst Item is: " + blockSet[0].childNodes[0].nodeValue);
    }
    .block-default {
    
      width: 100px;
    
      height: 50px;
    
      background-color: green;
    
      border: 1px solid red;
    
      padding: 10px;
    
    }
    
    .block-selected {
    
      width: 100px;
    
      height: 50px;
    
      background-color: blue;
    
      border: 1px solid white;
    
      padding: 10px;
    
    }
    
    
    
    BLOCK1
    BLOCK2
    BLOCK3
    BLOCK4
    BLOCK5
    BLOCK6
    BLOCK7
    BLOCK8

    Or we can use it use document.getElementsByClassNamewith for loopso a close alternative is querySelectorAll as Rick Hitchcock answered it.

    function myFunction() {
      var blockSet = document.querySelectorAll('.block-default');
      blockSet[0].className = "block-selected";
      blockSet[1].className = "block-selected";
      blockSet[2].className = "block-selected";
      blockSet[3].className = "block-selected";
      blockSet[4].className = "block-selected";
      blockSet[5].className = "block-selected";
      blockSet[6].className = "block-selected";
      blockSet[7].className = "block-selected";
    }
    .block-default {
      width: 100px;
      height: 50px;
      background-color: green;
      border: 1px solid red;
      padding: 10px;
    }
    .block-selected {
      width: 100px;
      height: 50px;
      background-color: blue;
      border: 1px solid white;
      padding: 10px;
    }
    
    
    
    BLOCK1
    BLOCK2
    BLOCK3
    BLOCK4
    BLOCK5
    BLOCK6
    BLOCK7
    BLOCK8

    I hope my post it helps, let me know if you have any question.

提交回复
热议问题