简体   繁体   English

为什么不使用 (target) 时样式规则不适用?

[英]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.

相关问题 当我在 Javascript 中调用它时,样式不适用于我的代码的一部分? - The Style don't apply in a part of my code when I call it in Javascript? CSS 规则不起作用,除非使用 !important - CSS rules don't work unless !important is used 为什么类不适用于Angular指令? - Why don't classes apply to Angular directives? 为什么 styles 不适用于危险的SetInnerHTML - why styles don't apply on dangerouslySetInnerHTML 如果在 JS 中设置了样式,则 CSS:hover 不适用 - CSS :hover don't apply if style is set in JS 为什么apply 不能与classList 一起使用? - Why can't apply be used with classList? 同构样式加载器为什么引发TypeError:与CSS-Modules一起使用时,无法读取未定义的属性“ apply” - Why does isomorphic-style-loader throw a TypeError: Cannot read property 'apply' of undefined when being used in unison with CSS-Modules 为什么浏览器在onclick的值中使用任何其他单词代替'javascript'时不会抛出错误? - Why don't browsers throw an error when any other word is used in place of 'javascript' in the value of onclick? 当我在JavaScript上使用标签时,为什么不能通过CSS为标签设置样式? - Why can't I style tags via css when I used them on a javascript? 当我用箭头功能重写时,为什么不能将IIFE与Douglas Crockford的风格一起使用? - Why can't an IIFE be used with Douglas Crockford's style when I rewrite it with an arrow function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM