简体   繁体   中英

Why style rules don't apply when (target) is not used?

I hope everyone is doing well

When is used this code, style rules work

document.getElementById("heading").addEventListener("click", function (e) {
    e.target.style.color = 'purple';
    e.target.style.textAlign = 'center';
    e.target.innerText = 'Code With Harry';
});

But when i remove.target from the code, the style rules don't work and it gives me error I can't understand the reason behind it I would really appreciate if anyone can help me out..

Thanks

you can't remove the target because the e.target is the element that you need to style it. for better understanding console it.

target is the element that triggered the event (eg, the user clicked on)

see more Event.target

document.getElementById("heading").addEventListener("click", function (e) {
  console.log(e.target);
  e.target.style.color = "purple";
  e.target.style.textAlign = "center";
  e.target.innerText = "Code With Harry";
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM