onclick=quot;quot; vs event handler(onclick=“vs 事件处理程序)
问题描述
如果我想执行一个函数,我更喜欢做内联js:
If I want a function to be executed, I prefer doing inline js:
<p id="element" onclick="doSomething();">Click me</p>
因为它更容易调试.
但是,我听到有人说不要使用内联 js,然后:
However, I hear people saying not to use inline js, and do:
document.getElementById('element').onclick = doSomething;
为什么推荐js事件监听器?
Why is the js event listener recommended?
推荐答案
基本上它与整体有关,我相信保持一切独立.所以保持 HTML/CSS/JS 都是分开的.它使您的 HTML 更整洁,而且我认为更易于导航.
Basically it has to do with the whole keep everything separate I believe. So keep HTML/CSS/JS all separate. It makes your HTML tidier and, I think, easier to navigate without.
然后,当/如果您需要进行大的更改时,您有足够的空间,无论如何都必须将内联 JS 转移到外部文件,或者如果您想将相同的功能应用于多个按钮,那么代码就更少了.更少的代码是一个更快乐的地方
Then when/if you need to make large changes, you have ample space with having to shift the inline JS to an external file anyway OR if you want to apply the same function to more than one button, then it's less code. And less code is a happier place
如果你有正确的 JS 文件,并且有完整的文档记录,那么由外部人员导航它们会更容易
If you have your JS files properly, and thoroughly documented then navigating them by an outside person is made eaiser
这篇关于onclick=“"vs 事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:onclick=“"vs 事件处理程序


基础教程推荐
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01