JQuery over JavaScript 为什么以及何时使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7266067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery over JavaScript Why and When to use?
提问by vol7ron
I'm a student and also new to JQuery. I want to know the difference between JQuery vs JScript. How we decide what to use and when to use those two technology.
我是一名学生,也是 JQuery 的新手。我想知道 JQuery 与 JScript 之间的区别。我们如何决定使用什么以及何时使用这两种技术。
Considering
考虑
- Performance
- Technology
- 表现
- 技术
回答by Abe Miessler
jQuery isJavaScript. It is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery.
jQuery是JavaScript。它是一个 JavaScript 库,因此它在 JavaScript 之上运行。它不能单独存在,所以你不能使用一个而不是另一个。您可以只使用 JavaScript 或 JavaScript 和 jQuery。
jQuery is designed to make many JavaScript development tasks much easier.
jQuery 旨在使许多 JavaScript 开发任务变得更加容易。
Use jQuery when it will significantly reduce your development time, and you can afford the extra overhead of downloading the library.
当 jQuery 会显着减少您的开发时间时使用它,并且您可以负担下载库的额外开销。
A lot of people automatically include jQuery without considering the fact that it might not make the particular development task at hand much easier. This is a bad habit to get into...
许多人会自动包含 jQuery,而没有考虑到它可能不会使手头的特定开发任务变得更容易这一事实。这是一个坏习惯...
Personally my most common uses are:
就个人而言,我最常见的用途是:
- Complex element selection
- Animation
- Event handling
- 复杂元素选择
- 动画片
- 事件处理
There is a humorous Photoshopped screen shot of a "Stack Overflow" post that pokes some fun at the overuse of jQuery. Enjoy:
有一个“堆栈溢出”帖子的幽默 Photoshop 屏幕截图,它对 jQuery 的过度使用提出了一些乐趣。享受:
回答by vol7ron
I was initially against jQuery and, for new programmers, I still am. It is a pure JavaScript framework that has several benefits; here are some of the major ones:
我最初反对 jQuery,对于新程序员,我仍然反对。它是一个纯 JavaScript 框架,有几个好处;以下是一些主要的:
- crossbrowser support
- easy element selection
- customizable plugins
- large support community
- very popular
- 跨浏览器支持
- 简单的元素选择
- 可定制的插件
- 大型支持社区
- 非常受欢迎
Some would argue that it is large, which could affect your web performance... and they would be right. However, because of its popularity, it is likely to have already been used on some site you've visited before. Most browsers these days are good about caching scripts/images, so that download hit is reduced over time.
有些人会争辩说它很大,这可能会影响您的网络性能……他们是对的。但是,由于它的受欢迎程度,它很可能已经在您之前访问过的某些站点上使用过。如今,大多数浏览器都擅长缓存脚本/图像,因此随着时间的推移,下载次数会减少。
On the other hand, many new programmers that rely on the framework become very dependent on it, so much so that they lose their ability to evaluate the best tool for the job, and use the most familiar tool that they're used to. These same programmers lose their ability to properly debug and become reliant on an API that has no standard.
另一方面,许多依赖该框架的新程序员变得非常依赖它,以至于他们失去了评估最适合工作的工具的能力,而无法使用他们习惯的最熟悉的工具。这些程序员失去了正确调试的能力,并变得依赖于没有标准的 API。
What I like using it for is it's cross browser support, which is great for event handling; and whenever you apply dynamic styling to elements on the page (or really anything that requires you needing a more advanced set of elements). Element selection is it's #1 tool, where you can select elements by id, type, attribute, classname, tagname, hierarchical relationship,... all sorts of ways.
我喜欢使用它的原因是它的跨浏览器支持,这对于事件处理非常有用;以及每当您将动态样式应用于页面上的元素(或任何需要您需要更高级元素集的元素)时。元素选择是它的#1 工具,您可以在其中通过 id、类型、属性、类名、标记名、层次关系等各种方式选择元素。
回答by thomasrutter
I want to know the difference between JQuery vs JScript.
我想知道 JQuery 与 JScript 之间的区别。
JavaScriptis a scripting language for the web. Microsoft refers to their implementation of it as "JScript" but in terms of syntaxit's pretty much the same.
JavaScript是一种用于 Web 的脚本语言。Microsoft 将他们的实现称为“JScript”,但在语法方面几乎相同。
jQueryis a software library, written in JavaScript, whose intention is to help JavaScript developers when writing code that is to be run in a web page.
jQuery是一个用 JavaScript 编写的软件库,其目的是帮助 JavaScript 开发人员编写要在网页中运行的代码。
How we decide what to use and when to use those two technology.
我们如何决定使用什么以及何时使用这两种技术。
JavaScript (including jQuery) can be used to add interactivity to a web page beyond that which is possible merely with HTML and CSS. This allows for a more "application-like" experience for the user. Many see it as a bad idea to make a web page which depends on JavaScript, and won't run without it, and insist that JavaScript should only enhance the user experience. However, websites which won't work without JavaScript do exist and are becoming more common.
JavaScript(包括 jQuery)可用于向网页添加超出仅使用 HTML 和 CSS 可能实现的交互性。这为用户提供了更“类似应用程序”的体验。许多人认为制作一个依赖于 JavaScript 的网页是一个坏主意,没有它就无法运行,并坚持认为 JavaScript 应该只增强用户体验。然而,没有 JavaScript 就无法运行的网站确实存在并且变得越来越普遍。
jQuery runs in JavaScript, and is written in JavaScript. If you are writing JavaScript for a web page, it is often a good idea to also use a library (or framework) like jQuery to make the task of browser-compatibility that much easier. It is like a pre-packaged set of JavaScript routines that you may have otherwise needed to write yourself, packaged in an easy-to-use way.
jQuery 在 JavaScript 中运行,并且是用 JavaScript 编写的。如果您正在为网页编写 JavaScript,那么最好同时使用 jQuery 之类的库(或框架)来简化浏览器兼容性任务。它就像一组预先打包的 JavaScript 例程,您可能需要自己编写,以易于使用的方式打包。
A drawback of using jQuery is that it is a relatively large file size, which does matter on the web. Some would argue that if you are not using enough of jQuery to justify its file size on your site, you should consider something else (such as a more modular framework, or not using a library/framework at all).
使用 jQuery 的一个缺点是它的文件相对较大,这在网络上很重要。有些人会争辩说,如果您没有使用足够的 jQuery 来证明其在您的站点上的文件大小是合理的,那么您应该考虑其他东西(例如更模块化的框架,或者根本不使用库/框架)。
回答by shadylane
It's perhaps a little redundant now but 'JScript' was originally the name of the Microsoft implementation of Javascript in old versions of Internet Explorer. Thankfully it's long since disappeared although if you find references to it in older literature just replace with 'Javascript'.
它现在可能有点多余,但“JScript”最初是旧版本 Internet Explorer 中 Javascript 的 Microsoft 实现的名称。值得庆幸的是,它早已消失,但如果您在旧文献中找到对它的引用,只需将其替换为“Javascript”即可。
Back to the questions, if you're new to this, I'd strongly recommend going for JQuery (or another Javascript library) instead of raw Javascript. You can do very complex things with one line of JQuery that would take a lot of coding and debugging if you were to use raw Javascript. Ultimately you will need an understanding of the underlying language to take full advantage of JQuery but if you want something you can deploy today, start with JQuery rather than Javascript.
回到问题上来,如果您不熟悉这个,我强烈建议您使用 JQuery(或其他 Javascript 库)而不是原始 Javascript。您可以使用一行 JQuery 来完成非常复杂的事情,如果您要使用原始 Javascript,这将需要大量的编码和调试。最终,您需要了解底层语言才能充分利用 JQuery,但如果您想要现在可以部署的东西,请从 JQuery 而不是 Javascript 开始。
回答by user2234591
I have been recently struggling with the same question, jQuery or JavaScript. Coming from a coding background that includes several old school programming languages (C, perl, ksh, and more recently Java and C#), I am finding that JavaScript is MUCH easier to quickly understand than jQuery. From a support perspective, the ease of understanding the code is more important than less typing when creating the code. The one answer here, that does have me reconsidering the use of jQuery, is browser compatibility. This one significantly impacts future application support. Based on my experience (over 20 years as a software engineer) the application life cycle spends a lot more time in support than it does in development.
我最近一直在为同样的问题苦苦挣扎,jQuery 或 JavaScript。来自包括几种老式编程语言(C、perl、ksh,以及最近的 Java 和 C#)的编码背景,我发现 JavaScript 比 jQuery 更容易快速理解。从支持的角度来看,在创建代码时,易于理解代码比减少键入更重要。一个让我重新考虑使用 jQuery 的答案是浏览器兼容性。这会显着影响未来的应用程序支持。根据我的经验(作为软件工程师超过 20 年),应用程序生命周期在支持上花费的时间比在开发上花费的时间要多得多。