简体   繁体   English

为什么人们使用jQuery进行基本操作?

[英]Why do people use jQuery for basic operations?

I am a JS programmer and I have been experimenting with jQuery a lot but have run into a couple puzzling aspects. 我是一名JS程序员,我一直在尝试使用jQuery,但遇到了一些令人费解的问题。

I feel like people use jQuery for much more than necessary. 我觉得人们使用jQuery远远超过必要。 I really just want to know why picking jQuery may be better than using just pure JS. 我真的只是想知道为什么选择jQuery可能比使用纯JS更好。

I know it makes sense for webfx like the animate and fades but for things like adding event listeners it seems just as easy to use 我知道webfx就像动画和淡化一样有意义但是对于添加事件监听器之类的东西来说它似乎同样易于使用

obj = document.getElementByID(_ID_);
obj.addEventListener("mousedown"...);

An example of this is the answer I found on StackOverflow earlier today about performing an action for highlighted text. 这方面的一个例子是我今天早些时候在StackOverflow上找到的关于为突出显示的文本执行操作的答案。 Get the Highlighted/Selected text 获取突出显示/选定的文本

In the example linked in the answer at http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html的答案中链接的示例中

The guy uses the bind function to the document. 该人使用绑定功能到文档。 Why use bind rather than addEventListener. 为什么使用bind而不是addEventListener。 Also with jQuery everything needs to be included in the .ready() method how is this better than (or why choose it over) 另外,对于jQuery,一切都需要包含在.ready()方法中,这比(或者为什么选择它)更好

document.addEventListener('load', function () { ... }, false);

There are other times I have seen jQuery used that puzzled me, I hope you guys can shine some light on it for me. 还有一次我看到jQuery使用这让我很困惑,我希望你们能为我发光一些。

People use jQuery because it's simpler, easier, and more powerful, and because it helps them forget about IE. 人们使用jQuery因为它更简单,更容易,更强大,并且因为它可以帮助他们忘记IE。

To answer your specific questions: 回答您的具体问题:

  1. Otherwise, you need to call attachEvent for IE. 否则,您需要为IE调用attachEvent
    Also, jQuery event handling has simpler syntax, and supports live events. 此外,jQuery事件处理具有更简单的语法,并支持实时事件。

  2. jQuery does not require you to put everything in a ready handler; jQuery不要求你把所有东西放在一个ready处理程序中; it's actually better to move your code to the bottom of the page and execute it immediately. 实际上,将代码移动到页面底部并立即执行它会更好。
    Unlike document.addEventListener('load', ...) , jQuery's ready event will not wait for images to load. document.addEventListener('load', ...) ,jQuery的ready事件不会等待加载图像。
    Also, it works in IE, and it will still run your code even if the document already loaded. 此外,它在IE中工作,即使已加载文档,它仍将运行您的代码。

Well, on() is quite useful because addEventListener() is only supported from Internet Explorer 9 onwards. 好吧, on()非常有用,因为只能从Internet Explorer 9开始支持addEventListener()

The reverse is true for eg the mouseenter and mouseleave events: those are only supported by IE (so far), and jQuery emulates them in other browsers. 反之亦然,例如mouseentermouseleave事件:那些只受IE支持(到目前为止),jQuery在其他浏览器中模拟它们。

我最大的原因是跨浏览器兼容性,尤其是事件处理。

The idea behind jquery is "Write less, do more". jquery背后的想法是“少写,多做”。

With trivial examples the difference in the amount of code written is small, but as you start to write more complicated stuff the power of jquery becomes apparent. 使用简单的示例,编写的代码量的差异很小,但是当您开始编写更复杂的东西时,jquery的功能变得明显。

There's also a lot of cross-browser stuff built into jQuery, which means you have to worry less about browser-specific code. jQuery中还内置了许多跨浏览器的东西,这意味着你不必担心特定于浏览器的代码。

因为jQuery开发人员比我更聪明,并且经常会实现更有效的算法来完成我正在尝试做的事情。

I have more confidence in jQuery being platform-independent than I do with mere JavaScript. 我对jQuery独立于平台的信心比单纯JavaScript更有信心。 For that reason, I'm tempted to use jQuery as much as I can. 出于这个原因,我很想尽可能多地使用jQuery。 And I think jQuery is good and stable enough of a platform to abstract away some of the browser-specific complications that way. 而且我认为jQuery是一个很好且稳定的平台,可以通过这种方式抽象出一些特定于浏览器的复杂问题。

因为它是交叉兼容的并得到很好的支持(考虑XHR请求)......但是在某些项目中,由于缺乏“资产”方法,我最好使用MooTools代替JQuery。

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

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