How to access HTML element without ID?

后端 未结 6 1542
暖寄归人
暖寄归人 2020-11-30 05:39

For instance in the snippet below - how do I access the h1 element knowing the ID of parent element (header-inner div)?

&
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 06:27

    If you are sure that there is only one H1 element in your div:

    var parent = document.getElementById('header-inner');
    var element = parent.GetElementsByTagName('h1')[0];
    

    Going through descendants,as Shog9 showed, is a good way too.

提交回复
热议问题