Get All Elements By ClassName and Change ClassName

前端 未结 5 2087
深忆病人
深忆病人 2020-11-29 08:59

I would like to...

  1. Scan the document for all elements that have a certain class name
  2. Perform some key functions on the innerHTML of that element
相关标签:
5条回答
  • 2020-11-29 09:41

    Most DOM functions that return a list of elements, return a NodeList rather than an array. The biggest difference is that a NodeList is typically live, meaning that changing the document can cause nodes to magically appear or disappear. That can cause nodes to move around a bit, and throw off a loop that doesn't account for it.

    Rather than turning the list into an array or whatever, though, you could simply loop backwards on the list you get back.

    function example()
    {
        var elements = document.getElementsByClassName("exampleClass");
    
        for(var i = elements.length - 1; i >= 0; --i)
        {
            // PERFORM STUFF ON THE ELEMENT
            elements[i].className = "exampleClassComplete";
    
            // elements[i] no longer exists past this point, in most browsers
        }   
    }
    

    The liveness of a NodeList won't matter at that point, since the only elements removed from it will be the ones after the one you're currently on. The nodes that appear before it won't be affected.

    0 讨论(0)
  • 2020-11-29 09:45

    You can also, use 2 arrays, push data into the first array, then do what you need to do [like changing element's class]:

    function example()
    {
        var elementArray=[];
        var elementsToBeChanged=[];
        var i=0;
    
        elementArray = document.getElementsByClassName("exampleClass");
    
        for( i = 0; i < elementArray.length; i++){
            elementsToBeChanged.push(elementArray[i]);
        }
    
        for( i=0; i< elementsToBeChanged.length; i++)
        {
            elementsToBeChanged[i].setAttribute("class", "exampleClassComplete");
        }
    }
    
    0 讨论(0)
  • 2020-11-29 09:49

    The problem is that the NodeList returned to you is "live" - it changes as you alter the class name. That is, when you change the class on the first element, the list is immediately one element shorter than it was.

    Try this:

      while (elementArray.length) {
        elementArray[0].className = "exampleClassComplete";
      }
    

    (There's no need to use setAttribute() to set the "class" value - just update the "className" property. Using setAttribute() in old versions of IE wouldn't work anyway.)

    Alternatively, convert your NodeList to a plain array, and then use your indexed iteration:

      elementArray = [].slice.call(elementArray, 0);
      for (var i = 0; i < elementArray.length; ++i)
        elementArray[i].className = "whatever";
    

    As pointed out in a comment, this has the advantage of not relying on the semantics of NodeList objects. (Note also, thanks again to a comment, that if you need this to work in older versions of Internet Explorer, you'd have to write an explicit loop to copy element references from the NodeList to an array.)

    0 讨论(0)
  • 2020-11-29 09:50

    Another approach could be to use a selector:

    var arr = document.querySelectorAll('.exampleClass');
    for (var i=0;i<arr.length;i++) {
        arr.innerHTML = "new value";
    }
    

    Although it's not compatible with older browsers, it can do the trick as well if the webcontent is for modern browsers.

    0 讨论(0)
  • 2020-11-29 10:01

    Another possibility, which should also be cross-browser, use a hand taylored getElementsByClassName which returns a fixed node list as an array. This should support from IE5.5 and upwards.

    function getElementsByClassName(node, className) {
        var array = [],
            regex = new RegExp("(^| )" + className + "( |$)"),
            elements = node.getElementsByTagName("*"),
            length = elements.length,
            i = 0,
            element;
    
        while (i < length) {
            element = elements[i];
            if (regex.test(element.className)) {
                array.push(element);
            }
    
            i += 1;
        }
    
        return array;
    }
    
    0 讨论(0)
提交回复
热议问题