CSS pseudo-selector to select the current element within querySelector?

前端 未结 1 1905
情话喂你
情话喂你 2021-01-17 11:02

What\'s the CSS pseudo-selector to select an element\'s self?

For example, this does not work:

Array.prototype.map.call(document.querySelectorAll(\'.         


        
1条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-17 11:16

    In some of the latest browsers (Chrome, Firefox 32+, Opera 15+, and Safari 7.0+) you can use the :scope selector in calls to querySelector and querySelectorAll:

    let result = [...document.querySelectorAll('.program_record_outer')].map(
      programBox => programBox.querySelector(':scope > div')
    )
    
    console.log(result)
    1
    2
    3

    0 讨论(0)
提交回复
热议问题