javascript get child by id

后端 未结 4 1230
广开言路
广开言路 2020-12-13 17:23
Test
child

I want to change the style of the child div

4条回答
  •  醉梦人生
    2020-12-13 17:44

    If the child is always going to be a specific tag then you could do it like this

    function test(el)
    {
     var children = el.getElementsByTagName('div');// any tag could be used here..
    
      for(var i = 0; i< children.length;i++)
      {
        if (children[i].getAttribute('id') == 'child') // any attribute could be used here
        {
         // do what ever you want with the element..  
         // children[i] holds the element at the moment..
    
        }
      }
    }
    

提交回复
热议问题