简体   繁体   English

Javascript:使用Jquery的bind()出价事件是否比仅执行onclick ='bla()'更好?为什么?

[英]Javascript: is biding events using Jquery's bind() better than simply doing the onclick='bla()' and why?

Is binding events using JQuery's bind() considered better than simply doing the onclick='bla(this)' and why ? 使用JQuery的bind()绑定事件是否比仅执行onclick ='bla(this)'更好,为什么?

Thanks ! 谢谢 !

Because Unobtrusive JavaScript (UJS) is the new normal. 因为非侵入式JavaScript(UJS)是新常态。

For me, the separation of markup from behavior is the biggest benefit, although there are other things that are handy about it, and everyone likes different aspects. 对我来说,将标记与行为分离是最大的好处,尽管还有其他一些方便的方面,而且每个人都喜欢不同的方面。

@Esailija correctly points out that jQuery makes UJS much easier, in terms of level of work, browser compatibility, browser workarounds, and so on. @Esailija正确地指出,jQuery在工作水平,浏览器兼容性,浏览器解决方法等方面使UJS 更加容易。 UJS doesn't depend on jQuery, but JS libraries have made it viable across a wider swath of functionality. UJS不依赖 jQuery,但是JS库使它在更广泛的功能中都可行。

With jQuery events you get 6 years worth of bug fixes, normalization of the event object, namespaces, support for events such as mouseenter, mouseleave, focusin and focusout which are originally proprietary IE events and event delegation. 使用jQuery事件,您可以获得6年的错误修复,事件对象的规范化,名称空间,对诸如mouseenter,mouseleave,focusin和focusout之类的事件的支持,这些事件原本是IE事件和事件委托。

With events attached with any sort of js (not just jQuery) you gain: 通过将事件与任何类型的js(不仅仅是jQuery)相关联,您将获得:

  • Useful object context 有用的对象上下文
  • Useful function scope 有用的功能范围
  • Ability to attach multiple handlers for the same event on the same element 能够在同一元素上为同一事件附加多个处理程序

And in case you are generating html dynamically: 并且如果您正在动态生成html:

  • Sanity as you don't have to concatenate strings to generate code, you just write the event handler code just like any other code without having to worry about how your strings end up by first being interpreted as a javascript string, then by html, and then as javascript code (yes, three phases. I didn't even get the following right on the first revision): 明智的做法是,不必将字符串连接起来即可生成代码,您只需像其他任何代码一样编写事件处理程序代码,而不必担心首先通过解释为javascript字符串,然后通过html来解释字符串的最终结果。然后作为javascript代码(是的,分为三个阶段。在第一版中,我什至没有以下内容):

     var html = '<div onclick="alert(\\''+someVar+'\\'+\\'hello\\');"></div>' 

    is not exactly easy to write. 并不是很容易写。 Compare to 相比于

      $("<div>", { click: function () { alert(someVar + "hello"); } }); 

In my experience there is a price to pay for abstractions which should be weighted against the benefits of using them. 以我的经验,必须为抽象付出一定的代价,而应该权衡使用它们的好处。

In the case of jQuery I have found that the time it saves me far outweighs any of the disadvantages. 对于jQuery,我发现节省的时间远远超过了任何缺点。 To give you an example, how would you code the following example in standard javascript. 举一个例子,您将如何用标准javascript编写以下例子。

$("ul").on("click", "li", function(){ });

This jQuery method attaches an event handler to all present and future li elements. 此jQuery 方法将事件处理程序附加到所有当前和将来的li元素。

Implementing this functionality is not a trivial task and by using jQuery you are using a high quality code base that attempts to abstract browser differences and helps you focus on programming. 实现此功能并非易事,通过使用jQuery,您正在使用高质量的代码库,该代码库试图抽象化浏览器差异并帮助您专注于编程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM