本文翻译自:jQuery.click() vs onClick

I have a huge jQuery application, and I'm using the below two methods for click events. 我有一个庞大的jQuery应用程序,并且我将以下两种方法用于点击事件。

First method 第一种方法

HTML 的HTML

<div id="myDiv">Some Content</div>

jQuery jQuery的

$('#myDiv').click(function(){//Some code
});

Second method 第二种方法

HTML 的HTML

<div id="myDiv" onClick="divFunction()">Some Content</div>

JavaScript function call JavaScript函数调用

function divFunction(){//Some code
}

I use either the first or second method in my application. 我在应用程序中使用第一种或第二种方法。 Which one is better? 哪一个更好? Better for performance? 性能更好? And standard? 和标准?


#1楼

参考:https://stackoom.com/question/qyyR/jQuery-click-与onClick


#2楼

Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent ). 使用$('#myDiv').click(function(){ 更好,因为它遵循标准的事件注册模型。(jQuery 内部使用addEventListenerattachEvent )。

Basically registering an event in modern way is the unobtrusive way of handling events. 基本上,以现代方式注册事件是处理事件的简便方法。 Also to register more than one event listener for the target you can call addEventListener() for the same target. 另外,要为目标注册多个事件侦听器,您可以为同一目标调用addEventListener()

var myEl = document.getElementById('myelement');myEl.addEventListener('click', function() {alert('Hello world');
}, false);myEl.addEventListener('click', function() {alert('Hello world again!!!');
}, false);

http://jsfiddle.net/aj55x/1/ http://jsfiddle.net/aj55x/1/

Why use addEventListener? 为什么要使用addEventListener? (From MDN) (来自MDN)

addEventListener is the way to register an event listener as specified in W3C DOM. addEventListener是注册W3C DOM中指定的事件侦听器的方法。 Its benefits are as follows: 其好处如下:

  • It allows adding more than a single handler for an event. 它允许为一个事件添加多个处理程序。 This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used. 这对于即使使用其他库/扩展也需要正常工作的DHTML库或Mozilla扩展特别有用。
  • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling) 当激活侦听器时,它可以使您对相位进行更细粒度的控制(捕获与冒泡)
  • It works on any DOM element, not just HTML elements. 它适用于任何DOM元素,而不仅仅是HTML元素。

More about Modern event registration -> http://www.quirksmode.org/js/events_advanced.html 有关现代事件注册的更多信息-> http://www.quirksmode.org/js/events_advanced.html

Other methods such as setting the HTML attributes , example: 其他方法,例如设置HTML属性 ,例如:

<button onclick="alert('Hello world!')">

Or DOM element properties , example: 或DOM元素属性 ,例如:

myEl.onclick = function(event){alert('Hello world');};

are old and they can be over written easily. 很老,可以很容易地改写。

HTML attribute should be avoided as It makes the markup bigger and less readable. 应避免使用HTML属性,因为它会使标记更大且可读性更差。 Concerns of content/structure and behavior are not well-separated, making a bug harder to find. 对内容/结构和行为的关注没有很好地分开,这使得更难以发现错误。

The problem with the DOM element properties method is that only one event handler can be bound to an element per event. DOM元素属性方法的问题是每个事件只能将一个事件处理程序绑定到一个元素。

More about Traditional event handling -> http://www.quirksmode.org/js/events_tradmod.html 有关传统事件处理的更多信息-> http://www.quirksmode.org/js/events_tradmod.html

MDN Reference: https://developer.mozilla.org/en-US/docs/DOM/event MDN参考: https : //developer.mozilla.org/en-US/docs/DOM/event


#3楼

You could combine them, use jQuery to bind the function to the click 您可以将它们结合起来,使用jQuery将函数绑定到click

<div id="myDiv">Some Content</div>$('#myDiv').click(divFunction);function divFunction(){//some code
}

#4楼

The first method is to prefer. 第一种方法是首选。 It uses the advanced event registration model[s] , which means you can attach multiple handlers to the same element. 它使用高级事件注册模型 ,这意味着您可以将多个处理程序附加到同一元素。 You can easily access the event object, and the handler can live in any function's scope. 您可以轻松访问事件对象,并且处理程序可以存在于任何函数的作用域中。 Also, it is dynamic, ie it can be invoked at any time and is especially well-suited for dynamically generated elements. 而且,它是动态的,即可以随时调用,特别适合动态生成的元素。 Whether you use jQuery, an other library or the native methods directly does not really matter. 不管您直接使用jQuery,其他库还是直接使用本机方法都没有关系。

The second method, using inline attributes, needs a lot of global functions (which leads to namespace pollution) and mixes the content/structure (HTML) with the behavior (JavaScript). 第二种方法使用内联属性,需要大量全局函数(这会导致名称空间污染),并将内容/结构(HTML)与行为(JavaScript)混合在一起。 Do not use that. 不要使用它。

Your question about performance or standards can't be easily answered. 您对性能或标准的问题无法轻易回答。 The two methods are just completely different, and do different things. 两种方法完全不同,并且执行不同的操作。 The first one is mightier, while the second one is despised (considered bad style). 第一个比较强大,而第二个则被鄙视(被认为是不好的风格)。


#5楼

Most of the time, native JavaScript methods are a better choice over jQuery when performance is the only criteria, but jQuery makes use of JavaScript and makes the development easy. 在大多数情况下,当性能是唯一标准时,原生JavaScript方法是优于jQuery的更好选择,但是jQuery利用JavaScript并使开发变得容易。 You can use jQuery as it does not degrade performance too much. 您可以使用jQuery,因为它不会降低性能。 In your specific case, the difference of performance is ignorable. 在您的特定情况下,性能差异是可以忽略的。


#6楼

Neither one is better in that they may be used for different purposes. 两种方法都可以用于不同的目的,这两者都不是更好的选择 onClick (should actually be onclick ) performs very slightly better, but I highly doubt you will notice a difference there. onClick (实际上应该是onclick )的性能要好很多,但是我非常怀疑您会注意到那里的区别。

It is worth noting that they do different things: .click can be bound to any jQuery collection whereas onclick has to be used inline on the elements you want it to be bound to. 值得注意的是,它们执行不同的操作: .click可以绑定到任何jQuery集合,而onclick必须在要绑定到其的元素上内联使用。 You can also bind only one event to using onclick , whereas .click lets you continue to bind events. 您也只能使用onclick绑定一个事件,而.click允许您继续绑定事件。

In my opinion, I would be consistent about it and just use .click everywhere and keep all of my JavaScript code together and separated from the HTML. 在我看来,我会保持一致,只在各处使用.click并将我的所有 JavaScript代码放在一起并与HTML分开。

Don't use onclick . 不要使用onclick There isn't any reason to use it unless you know what you're doing, and you probably don't. 除非您知道自己在做什么,否则可能没有任何理由使用它。

jQuery.click()与onClick相关推荐

  1. jQuery中click和onclick的区别

    click()和onclick()的区别: 1.onclick是绑定事件,告诉浏览器在鼠标点击时候要做什么 click本身是方法作用是触发onclick事件,只要执行了元素的click()方法,就会触 ...

  2. jquery动态改变onclick属性导致失效的问题解决方法

    onclick属性失效的问题,相信很多的朋友都有遇到过吧,jquery动态改变onclick属性就会导致此问题的发生,解决方法如下,希望对大家有所帮助 代码如下: <li id="&q ...

  3. click和onclick的区别

    click与onclick区别 click是对象的方法,onclick是事件,当我们点击按钮是,首先触发的是事件,然后是方法: <html> <head><title&g ...

  4. chrome浏览器模拟手机端:jquery click()点击无效解决方法

    chrome浏览器模拟手机端:jquery click()点击无效解决方法 参考文章: (1)chrome浏览器模拟手机端:jquery click()点击无效解决方法 (2)https://www. ...

  5. js click 与 onclick 事件绑定,触发与解绑

    click 与 onclick 1.onclick 事件会在对象被点击时发生. <input id="btn1" type="button" οnclic ...

  6. click()和onclick()的区别

    click()和onclick()的区别: 1.onclick是绑定事件,告诉浏览器在鼠标点击时候要做什么 click本身是方法作用是触发onclick事件,只要执行了元素的click()方法,就会触 ...

  7. jquery click()方法模拟点击事件对a标签不生效的解决办法

    jquery click()方法模拟点击事件对a标签不生效的解决办法 参考文章: (1)jquery click()方法模拟点击事件对a标签不生效的解决办法 (2)https://www.cnblog ...

  8. js中click()与onclick()的区别

    由一个简单示例到 js中click()与onclick()的区别 之前朋友在学习js的时候遇到一个有意思的问题. 先贴一份代码说一下代码构成 这里是html结构 <ul><li> ...

  9. HTML中click()和onclick()的本质区别与案例和解释

    HTML中click()和onclick()的本质区别与案例和解释:分为四点: 第一点:英文翻译: On的意思为 :打开,接通,鼠标事件的触发条件: 表明onclick()代表的是一个事件:而在HTM ...

最新文章

  1. python装饰设备_Python: 装饰器的小例子
  2. Django-model中的Querysets
  3. 解决Word 2013, Word 2016的保存太慢的问题
  4. word中package提取器
  5. pl/sql函数学习
  6. windows 下怎样利用NET-SNMP 发送和接收trap
  7. qt on android 桌面鼠标事件,關於Qt on Android,程序安裝到手機,界面只占到一小部分。...
  8. 收藏一个有趣的帖子,现在的客户端真有点让人不安,难怪XSS。。。
  9. 鸿蒙 电视 安卓,华为鸿蒙2.0、EMUI 11齐发 打通手机、电视、PC全平台
  10. 【模板】匈牙利算法 二分图最大匹配题模板
  11. python爬虫取腾讯视频评论
  12. 真格量化-持仓量第n档卖方主力跟随策略
  13. HTML+CSS+JS实现 ❤️六边形圆柱弹性动画特效❤️
  14. C# 跨线程调用form控件技巧及byte[]与string型相互转换
  15. 离散数学 (屈婉玲)集合部分 笔记
  16. BZOJ 2339 卡农(组合数学)
  17. 同花顺炒股指标定制-K线只有红绿2个颜色怎么行?
  18. mac电脑有很多._开头的文件
  19. 路由器、交换机的基本配置 1
  20. 计算机毕业设计Python+django 宠物领养中心小程序(源码+系统+mysql数据库+Lw文档)

热门文章

  1. 看图说话:OpenGL模型矩阵和投影矩阵
  2. 算法----------字符串相乘(Java 版本)
  3. 算法--------字母异位词分组 (Java 版本)
  4. 图解 SQL 里的各种 JOIN
  5. androidstudio常见问题
  6. wireshark网络分析就这么简单_【读书笔记】2wireshark网络分析就这么简单——不同子网如何发送消息。...
  7. Hive简单案例WordCount
  8. webpack中loader加载器(打包非js模块)
  9. 大二暑假周进度报告之四
  10. CocosCreator上的游戏(调试)发布到微信小程序