I'm confused why $1
is firstBB
, The querySelector('.aa .bb')
call should execute under #root
element.
const $1 = document.querySelector('#root').querySelector('.aa .bb');
const $2 = document.querySelector('#root').querySelector('.aa').querySelector('.bb');
document.querySelector('#result').innerHTML = `$1 is ${$1.id}; $2 is ${$2.id}`
<div class="aa" id="root">
<div class="bb" id="firstBB">xxx</div>
<div class="aa">
<div class="bb" id="secondBB">xxx</div>
</div>
</div>
<div>Result: <span id="result"></span></div>
Chrome version: 116.0.5845.188
Is this the correct behavior of querySelector
?
#root
has a class calledaa
, I assume selection begins relative to itself. – Conduce