何时在html中使用全局事件处理程序属性成为“被认为是不良做法"? [英] When did using global event handler attributes within html become "considered bad practice"?

查看:70
本文介绍了何时在html中使用全局事件处理程序属性成为“被认为是不良做法"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关: 为什么要使用onClick()在HTML中是不好的做法吗? .另请参见 不引人入胜的JavaScript .

Related: Why is using onClick() in HTML a bad practice?. See also Unobtrusive JavaScript.

不引人注目的JavaScript 是使用JavaScript的通用方法 在网页中. 尽管该术语没有正式定义(强调)

Unobtrusive JavaScript is a general approach to the use of JavaScript in web pages. Though the term is not formally defined (emphasis added)

问题:

  1. 在什么时候确定使用onclick还是 html中的其他全局事件处理程序属性已被考虑" 一种不好的做法"?
  2. 由谁确定在html中使用全局事件处理程序属性是不好的做法"?
  3. 如果起草html并且在html中包含全局事件处理程序属性的用户知道事件处理程序 属性在html中的存在,这怎么可能 考虑",不良做法"?
  4. html的作曲家是否不应在html中包含事件处理程序属性?

  1. At what point was the determination made that using onclick or other global event handler attributes within html was "considered" a "bad practice"?
  2. By whom was the determination made that using global event handler attributes within html was a "bad practice"?
  3. If the user who drafts the html and includes global event handler attributes within the html is aware of the event handler attributes' presence within the html, how could this be "considered" "bad practice"?
  4. Should a composer of html not ever include an event handler attribute within html?

在任何情况下都不会认为在html 中包含事件处理程序属性是不良做法"吗?

Are there any cases where including event handler attributes within html would not be "considered" a "bad practice"?

htmljavascript的作曲者应该将谁的实践考虑"归于谁?也就是说,什么是 作曲家复习是否特定的参考点 目前的做法是考虑",坏",还不错"还是好"?或者, 是否有一个列表列出所有那些考虑因素", 考虑"可以实施的许多可能的做法 在htmljavascript?

Exactly whose "consideration" of a practice should the composer of the html, javascript defer to? That is, what is the reference point for a composer to review whether a particular practice is currently "considered" "bad", "not bad" or "good"? Or, is there a list which tallies all "considerations" of those that "consider" the many possible practices which could be implemented within html, javascript?

为澄清起见,试图确切地确定在html中使用全局事件处理程序的术语或短语不良做法"或被认为不良做法"何时以及由谁附加?什么是正式或伪官方文件或一组文件,列出参与导致坏"或好"称号的审议"过程的选民的投票?

For clarification, attempting to ascertain exactly when, and by whom, the term or phrase "bad practice" or "considered bad practice" was attached to the usage of global event handlers within html? And what is the official or pseudo-official document or set of documents which list the votes of those that participated in the "consideration" process that leads to a "bad" or "good" designation?

或者,考虑不良做法"一词主要是基于观点吗?

Or, is the term "considered bad practice" primarily opinion-based?

推荐答案

首先,社区"(一般JavaScript开发人员的集体智慧)认为这是不良做法.

First off, it is considered bad practice by "the community" -- the collective intelligence of average JavaScript developers.

根据定义,坏习惯不是任何人都可以决定的规则,就像您可能不擅长命名函数,函数太长或在各处使用全局变量一样,您可以使用JavaScript事件处理程序以HTML分配.

Bad practice, by definition, is not something that's a rule that's decided by anybody, just like you can be bad at naming functions or have too long functions or using global variables all over the place, you can have your JavaScript event handlers assigned in HTML.

没有民意调查,也没有W3C决定.这仅仅是许多人与他们一起经历的结果.

There was no poll and no W3C decision made. It's simply a result of many people's experience with them.

我认为主要原因是:

  • 事件处理程序需要是全局引用,因此所有内容 一般而言,这对全局变量不利.
  • 使用全局函数的地方不是很明显 如果您只查看JavaScript代码
  • 人们通常更喜欢保持HTML不受JavaScript和 将事件处理完全留在JavaScript领域.
  • 在JavaScript中保留事件处理分配可以做到这一点 您无需更改标记即可更新JavaScript代码.
  • 如果您有很多的话,它会变得很重复.例如,当为表中的每一行分配相同的事件处理程序时.
  • 您无法在HTML中完成所有事件分配,因此不可避免地 将在HTML中包含一些内容,而在JavaScript区域中包含一些内容,因此 感到特别混乱.
  • The event handlers need to be global references, so everything that's bad about global variables in general applies to them.
  • It's not very obvious where your global function is being used if you're only looking at the JavaScript code
  • People generally prefer to keep their HTML clean of JavaScript and leaving their event handling completely in JavaScript land.
  • Keeping the event handling assignment in JavaScript makes it so you don't have to change the markup to update your JavaScript code.
  • If you have a lot of them around, it can get pretty repetitive. For example, when assigning the same event handler to every row in a table.
  • You can't do all your event assignment in HTML, so you inevitably will have some in HTML and some in JavaScript land, which makes it feel extra messy.

但是,有很多教条,通常,当有一种新的做事方法时,羊群通常会朝着相同的方向奔波而避开其他任何意见.您会听到很多人说这很不好,而他们不必知道为什么这很糟糕,或者不知道如何解释他们为什么不好.

However, there's a lot of dogma, and generally when there's a new way to do things the sheep herd will generally just run around in the same direction eschewing any other opinions. You'll hear a lot of people saying it's bad without them necessarily knowing why it's bad or knowing how to explain why they're bad.

因此,如果您有一个简单的应用程序,并且只需要分配一个事件处理程序等,这是完全务实的,这是您的选择.

So, being completely pragmatic about it, if you have a simple application and you just need to assign a single event handler, etc., it's your choice.

所以IMO并没有什么错,如果您和您与之共事的人都喜欢这样做,那么请继续吧.

So IMO there's nothing wrong with it, and if you and the people you're working with like doing it that way, by all means, go ahead.

但是,是的,一般的JavaScript开发人员都认为这是不好的做法.没有权限.

But yes, it's considered bad practice by the average JavaScript developer. With no authority.

这篇关于何时在html中使用全局事件处理程序属性成为“被认为是不良做法"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆