Get first child node of specific type - JavaScript

后端 未结 4 1866
刺人心
刺人心 2021-02-04 23:19

Is it possible to get a child dom element, which will be the first child of a specific type.

For example in both of these examples I want to get the img ele

4条回答
  •  生来不讨喜
    2021-02-04 23:55

    var anchor = document.getElementById('mylink');
    var images = anchor.getElementsByTagName('img');
    var image = images.length ? images[0] : null;
    

提交回复
热议问题