Get id of div from its class name

前端 未结 2 1931
面向向阳花
面向向阳花 2021-01-04 02:42

How can I find an id based on its class just using javascript. I know this is easy with jQuery what is the solution.. using getElementsByTagName ?

2条回答
  •  天涯浪人
    2021-01-04 02:51

    document.getElementsByClassName('myClassName')[0].id
    

    or

    document.querySelector('.myClassName').id
    

提交回复
热议问题