在 HTML 中使用内联事件处理程序是不好的做法吗? [英] Is it bad practice to use inline event handlers in HTML?

查看:26
本文介绍了在 HTML 中使用内联事件处理程序是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用内联 JavaScript 事件处理程序是不好的,还是可以的?

Is it bad to use inline JavaScript event handlers, or is that fine?

在我打算使用它的页面上,我只会使用一次事件处理程序,所以在这种情况下使用内联事件处理程序是否可以接受,或者我应该在其中编写事件处理程序的代码<脚本>标签?

On the page I plan to use it on, I'm only going to use an event handler once, so is it acceptable to use an inline event handler in this case, or shall I write the code for the event handler within <script> tags?

推荐答案

如果您关心标记和维护的可读性,这是不好的做法,尤其是在更大的范围内,它可能会变得非常混乱 - 还要记住内联JS 永远不会像外部 js 文件那样被缓存,因此您在性能方面确实会受到一些影响,尤其是如果您滥用它

Its bad practice if your concern is readability in your mark-up and maintenance, especially on a larger scale it can get quite messy - also keep in mind the inline JS will never be cached like an external js file would so you do suffer a bit in regards to performance especially if you abuse it

阅读这篇文章以获得更多见解:http://robertnyman.com/2008/11/20/why-inline-css-and-javascript-code-is-such-a-bad-thing/

Read this article for more insight: http://robertnyman.com/2008/11/20/why-inline-css-and-javascript-code-is-such-a-bad-thing/

这篇关于在 HTML 中使用内联事件处理程序是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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