我有一個類hardware
,點擊時,我想改變背景顏色,當我點擊我的run
函數。但是,我的點擊一次將它們全部設置爲相同的顏色。點擊每個類項目的功能
我該如何區分每個hardware
與它們各自的點擊事件?
function run(){
var selector = document.getElementsByClassName('hardware');
for (var i = 0; i < selector.length; i++){
var index = selector[i];
selector[i].style.backgroundColor = "hotpink";
}
}
<section onclick="run()" class="hardware">some text, nth-child is one</section>
<section onclick="run()" class="hardware">some text, nth-child is two</section>
<section onclick="run()" class="hardware">some text, nth-child is three</section>
<section onclick="run()" class="hardware">some text, nth-child is four</section>
<section onclick="run()" class="hardware">some text, nth-child is five</section>