on(click) 事件委托 .forEach Array.from 只显示被点击的元素

on(click) event delegation .forEach Array.from only shows the clicked element

我正在尝试在用户单击动态 html 元素 (span) 时更改它们的背景颜色。

function showQuestion(){
    for(let i = 0; i < QuesPartA.length; i++){
      $(".questionBox").append('<div class="Question">Number '+ parseInt(i+1) +'</div>');
      QuesPartA[i]['option'].forEach(option => {
          $(".questionBox").append('<span class="pilihan">'+option +' </span><br>');  
      });
  } 

$(".secondBox").append('<a href="Listening Part A.html" class="btnToPartB">Continue to Part B</a>');
  };

$(".questionBox").on("click", "span.pilihan", function() {
  Array.from(document.getElementsByClassName("pilihan"))
  .forEach((element) => element.style.backgroundColor = "red");
    });

但是,我只希望点击的那个改变颜色,而不是所有的 span.pilihan,但是我上面的代码将颜色全部更改为红色。

有什么想法吗?

这样写:

 $(".questionBox").on("click", ".pilihan", function() {
  $(this).css("background", "red");
});