Get immediate first child element

后端 未结 1 1081
我在风中等你
我在风中等你 2020-12-01 08:55

I\'m writing a Chrome content script extension and I need to be able to target a specific element that, unfortunately, has no unique identifiers except its parent element.

相关标签:
1条回答
  • 2020-12-01 09:27

    Both these will give you the first child node:

    console.log(parentElement.firstChild); // or
    console.log(parentElement.childNodes[0]);
    

    If you need the first child that is an element node then use:

    console.log(parentElement.children[0]);
    

    Edit

    Ah, I see your problem now; parentElement is an array.

    If you know that getElementsByClassName will only return one result, which it seems you do, you should use [0] to dearray (yes, I made that word up) the element:

    var parentElement = document.getElementsByClassName("uniqueClassName")[0];
    
    0 讨论(0)
提交回复
热议问题