How to find element without use ID

前端 未结 5 389
甜味超标
甜味超标 2021-01-27 14:43

I have DOM element in array

this.object = 
     
\"color1 text1
5条回答
  •  广开言路
    2021-01-27 15:07

    You could use querySelectors:

    //find first element with class 'color' within a div
    //returns an element Node
    [yourdiv].querySelector('.color');
    //find all elements with class 'color' within a document
    //returns a nodeList
    document.querySelectorAll('.color');
    

    Or getElementsByClassName:

    //find first element with class 'color' within a div
    //returns a nodeList with 1 or more elements
    [yourdiv].getElementsByClassname('.color')[0];
    //find all elements with class 'color' within a document
    //returns a nodeList
    document.getElementsByClassName('.color');
    

提交回复
热议问题