简体   繁体   English

JQuery - 将GUI对象连接到事件的最佳方法?

[英]JQuery - Best way of wiring GUI objects to events?

Ever since JQuery came along a few years ago I've been using it in all my client-side scripts. 自从几年前JQuery出现以来,我一直在我的所有客户端脚本中使用它。 Initially I used the '$() syntax to grab and manipulate objects this, to me, is the 'old skool' paradigm of explicitly wiring up button events to functions: 最初我使用'$()语法来抓取和操作对象,对我来说,这是将按钮事件明确连接到函数的'旧skool'范例:

<button onClick='myFunction(this);' ...  />

with a related function: 具有相关功能:

function myFunction(obj){
    alert('this is how old-timers like me do it!')
}

Some of my colleagues prefer to do the attaching of scripts to events a la JQuery: 我的一些同事喜欢将脚本附加到事件jQuery:

$(document).ready(
    $("#myButton").click(
        function(event){
            alert('this is the newer JQuery way of doing things');
        }
    );
);

Initially I found the overly-bracey JQuery syntax with its reliance on anonymous functions etc. difficult to read, but my eyes and brain have adjusted so I'm happy with either way now. 最初我发现过于强烈的JQuery语法依赖于匿名函数等难以阅读,但我的眼睛和大脑已经调整好所以我现在对这两种方式感到满意。 I would however like our codeshop to be consistent. 但我希望我们的代码商店能够保持一致。 Does anyone have any opinions on which approach is 'better'? 有没有人对哪种方法“更好”有任何意见?

The jQuery version you have can be much smaller as well: 你拥有的jQuery版本也可以小得多:

$(function() {
  $("#myButton").click(function(){
    alert('this is the newer JQuery way of doing things');
  });
});

I'd go with this or any other unobtrusive route ( not inline handlers), there are just fewer issues overall, and maintenance can be much easier. 我会选择这个或任何其他不显眼的路线( 不是内联处理程序),整体问题更少,维护也容易。 Remember with this route there can be no JavaScript in your page, it can be loaded and cached once by your client meaning faster page loads as well. 请记住,使用此路由时,页面中不能包含 JavaScript,客户端可以加载和缓存一次,这意味着页面加载速度也会更快。

There are many things you just can't do properly (or cross-browser) inline as well, for example the very useful mouseenter and mouseleave (so they don't fire when entering a child) events are only available inline in IE. 有许多事情你不能正确地(或跨浏览器)内联,例如非常有用的mouseentermouseleave (因此它们在进入孩子时不会触发)事件只能在IE中内联。

I think the second approach is cleaner for several reasons : 我认为第二种方法更清洁,原因如下:

  • it really allows you to separate content and behaviour , that is you can define your content's structure first and only later take care of the specific behaviour... This is especially helpful if your team involves different technical profiles (somebody takes care of the raw HTML, somebody else is the Javascript expert ...) 它确实允许您分离内容和行为 ,也就是说,您可以先定义内容的结构,然后才能处理特定的行为...如果您的团队涉及不同的技术配置文件(有人负责原始HTML),这将特别有用,其他人是Javascript专家......)

  • This really helps maintenance , as you know where to look for the potentially buggy javascript if something goes wrong , instead of having to look through the whole HTML. 这确实有助于维护 ,因为如果出现问题,你知道在哪里寻找潜在的错误的javascript,而不是必须浏览整个HTML。

  • when your HTML is actually dynamic (generated on server-side, à la PHP or ASP.NET), it is actually quite painful to have to include those client-side elements in the HTML you generate... This is a very common source of errors, therefore it is good to have the javascript out of the way . 当你的HTML实际上是动态的(在服务器端生成,例如PHP或ASP.NET)时,在你生成的HTML中包含那些客户端元素实际上非常痛苦...这是一个非常常见的来源因此, 将javascript排除在外很好的

The 'jQuery' way enforces the separation between content and functionality, which is a Good Thing IMO. 'jQuery'方式强制实现内容和功能之间的分离,这是一件好事IMO。 It reduces the 'noise' in your content/markup. 它可以减少内容/标记中的“噪音”。 It also enables a programmer to concentrate on the scripting, whilst a 'marker-upper' concentrates on the HTML. 它还使程序员能够专注于脚本,而'marker-upper'则专注于HTML。

BTW, you don't have to be 'overly-bracey', or use anonymous functions if you don't want to: 顺便说一句,你不必是“过于布里斯”,或使用匿名函数,如果你不希望:

function myFunction(obj){
    alert('this is a compromise!')
}

$(function() {
    $("#myButton").click(myFunction);
});

The jQuery version is better because it separates behaviour (JavaScript) from structure (HTML). jQuery版本更好,因为它将行为(JavaScript)与结构(HTML)分开。

If brackets seem too many, you can use a language which compiles into JavaScript, such as CoffeeScript (http://jashkenas.github.com/coffee-script/): 如果括号看起来太多,您可以使用编译成JavaScript的语言,例如CoffeeScript(http://jashkenas.github.com/coffee-script/):

$ ->
  $("#myButton").click ->
    alert 'this is the newer JQuery way of doing things'

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

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