Repository navigation
|
老師好,各位同學好 devTool顯示: 無論更改Selector的方式:
都沒辦法順利選擇到元素 麻煩大家給我一些意見qq HTML
<div class="BTN">
<span>Button1</span>
<span>Button2</span>
</div>
CSS
<style>
.BTN {
display: inline-block;
margin-top: 5px;
}
.BTN > span {
background: yellowgreen;
padding: 20px;
border: 1px solid white
}
</style>
JS
<script>
const elements = document.querySelectorAll('.BTN span')
elements.style.background = "red"
</script> |
Answered by
aszx87410
Dec 8, 2023
Replies: 1 comment 1 reply
|
querySelectorAll 回傳的是一個很像陣列的東西,不是單一元素,所以 https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll#accessing_the_matches |
1 reply
Answer selected by
PT-HUANG
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
querySelectorAll 回傳的是一個很像陣列的東西,不是單一元素,所以
.style會是 undefined要自己用 forEach 去對每一個元素修改
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll#accessing_the_matches