1、jQuery实现脚本与页面的分离
在HTML代码中,我们还经常看到类似这样的代码:
即使validate()函数可以被放置在一个外部文件中,实际上我们依然是把页面与逻辑和事件混杂在一起。jQuery让你可以将这两部分分离。借助于jQuery,页面代码将如下所示:
代码如下:
$("myform").submit(function(){
...your code here
)}
这样我们可以实现灵活性非常强的清晰页面代码。jQuery让JavaScript代码从HTML页面代码中分离出来,就像数年前CSS让样式代码与页面代码分离开一样。
2、最少的代码做最多的事情
最少的代码做最多的事情,这是jQuery的口号,而且名副其实。使用它的高级selector,开发者只需编写几行代码就能实现令人惊奇的效果。开发者无需过于担忧浏览器差异,它除了还完全支持Ajax,而且拥有许多提高开发者编程效率的其它抽象概念。jQuery把JavaScript带到了一个更高的层次。以下是一个非常简单的示例:
代码如下:
$("p.neat").addClass("ohmy").show("slow");
通过以上简短的代码,开发者可以遍历“neat”类中所有的 元素,然后向其增加“ohmy”类,同时以动画效果缓缓显示每一个段落。开发者无需检查客户端浏览器类型,无需编写循环代码,无需编写复杂的动画函数,仅仅通过一行代码就能实现上述效果。
3、性能
在大型JavaScript框架中,jQuery对性能的理解最好。尽管不同版本拥有众多新功能,其最精简版本只有18KB大小,这个数字已经很难再减少。jQuery的每一个版本都有重大性能提高。本篇文章讲的是jQuery未经过任何优化的原始速度。如果将其与新一代具有更快 JavaScript引擎的浏览器(如火狐3和谷歌Chrome)配合使用,开发者在创建富体验Web应用时将拥有全新速度优势。
4、它是一个“标准”
之所以使用引号,是以为jQuery并非一个官方标准。但是业内对jQuery的支持已经非常广泛。谷歌不但自己使用它,还提供给用户使用。另外戴尔、新闻聚合网站Digg、WordPress、Mozilla和许多其它厂商也在使用它。微软甚至将它整合到Visual Studio中。如此多的重量级厂商支持该框架,用户大可以对其未来放心,大胆的对其投入时间。
5、插件
基于jQuery开发的插件目前已经有大约数千个。开发者可使用插件来进行表单确认、图表种类、字段提示、动画、进度条等任务。jQuery社区已经成长为一个生态系统。这一点进一步证明了上一条理由,它是一个安全的选择。而且,jQuery正在主动与“竞争对手”合作,例如Prototype。它们似乎在推进JavaScript的整体发展,而不仅仅是在图谋一己之私。
6、节省开发者学习时间
当然要想真正学习jQuery,开发者还是需要投入一点时间,尤其是如果你要编写大量代码或自主插件的话,更是如此。但是,开发者可以采取“各个击破”的方式,而且jQuery提供了大量示例代码,入门是一件非常容易的事情。我建议开发者在自己编写某类代码前,首先看一下是否有类似插件,然后看一下实际的插件代码,了解一下其工作原理。简而言之,学习jQuery不需要开发者投入太多,就能够迅速开始开发工作,然后逐渐提高技巧。
7、让JavaScript编程变得有趣
I find using jQuery a lot of fun. It is simple and powerful, and developers can quickly get the results they want. It solves many JavaScript problems and problems. I used to hate JavaScript programming but now absolutely love it. Through some basic improvements, developers can really think about developing the next generation of web applications and no longer worry about poor languages or tools. I believe in its mantra of "least code doing the most".
The shortcomings of jQuery
Of course, jQuery is not perfect. The shortcomings of jQuery that I have discovered so far include:
◆ Not backward compatible. Each new version is not compatible with earlier versions. For example, some new versions no longer support certain selectors, but the new version of jQuery does not retain support for them, but simply removes them. This may affect code or plug-ins that developers have already written.
◆ Plug-in compatibility. Similar to the previous point, when a new version of jQuery is launched, if developers want to upgrade, it depends on whether the plug-in author supports it. Often, existing plugins may not work properly with the latest jQuery version. The more plugins a developer uses, the higher the chance of this happening. I once had to modify a third-party plug-in myself in order to upgrade to jQuery 1.3.
◆ When using multiple plug-ins on the same page, it is easy to encounter conflicts, especially when these plug-ins rely on the same event or selector. Although this is not a problem with jQuery itself, it is indeed a problem that is difficult to debug and solve.
◆ jQuery stability. It didn't crash my browser, and I'm referring to its release strategy here. Just a few days after the release of jQuery version 1.3, a bugfix version 1.3.1 was released. They also removed support for certain features, which may affect the proper functioning of many codes. I hope similar modifications will not happen again.
◆ In large frameworks, the jQuery core code base has relatively poor support for animations and special effects. But actually this is not a problem. There is currently a separate jQuery UI project and numerous plugins to make up for this.
If you are still hesitating whether to learn a JavaScript framework and are confused about which framework to choose, then I recommend you to choose jQuery. This is not to say that other frameworks are bad, but jQuery is probably the safest and most rewarding choice.