Rails每周闲碎(九): JavaScript

本文介绍了在Rails框架中如何使用jQuery替代Prototype,并通过jRails实现Prototype的辅助方法。对比了jQuery与Prototype的不同之处,如$(document).ready()与window.onload的区别,以及jQuery提供的便利功能如serialize。

1. Caching multiple javascript into one

 

    javascript_include_tag :all, :cache => true

    javascript_include_tag "prototype", "cart", "checkout", :cache => "shop"

 

2. jQuery

 

    jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.

 

    Rails默认使用的ajax框架是prototype,结合prototype,Rails提供了一些非常方便的prototype helper方法,比如:remote_form等。如果在Rails中选择使用jQuery库,不用担心使用不了这些helper方法,因为jRails 为这些方法提供了jQuery版本。

 

    jRails is a drop-in jQuery replacement for Prototype /script.aculo.us on Rails. Using jRails, you can get all of the same default Rails helpers for javascript functionality using the lighter jQuery library.

 

   用jQuery实现的一些工具也很好用,比如greybox


 

3. prototype helper methods

 

    It's strong by providing lots of helper method like observe_field, with lots of options like :success, :condition, :before.

 

    如果使用JQuery,也不需要担心这些方法没法使用。JRails 为这些方法实现了JQuery版本。

 

4. jquery的$(document).ready()

 

    与window.onload的区别 : $(document).ready()是在DOM结构载入完后执行的,而window.onload是在所有文件 都加载完后执行的。

 

5. jquery的serialize

 

    Serializes a set of input elements into a string of data.   http://docs.jquery.com/Ajax/serialize

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值