Get first child node of specific type - JavaScript

后端 未结 4 1877
刺人心
刺人心 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:33

    Here is a javascript function

    function getFirst(con,tag)
    {
        return con.getElementsByTagName(tag)[0];
    }
    
    
    //html markup
    
    
    
    //test
    var con=document.getElementById('mylink');
    var first=getFirst(con,'img');
    

提交回复
热议问题