我有一个类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>