[英]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..但是当我从代码中删除.target时,样式规则不起作用,它给了我错误我无法理解其背后的原因,如果有人能帮助我,我将不胜感激..
Thanks谢谢
you can't remove the target because the e.target is the element that you need to style it.您无法删除目标,因为 e.target 是您需要为其设置样式的元素。 for better understanding console it.
为了更好地理解控制台它。
target is the element that triggered the event (eg, the user clicked on) target 是触发事件的元素(例如,用户点击)
see more Event.target查看更多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";
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.