月份: 2013-05

转一篇, JQuery 与 Prototype 的使用异同

页面载入

// JQuery
	$ ( document ). ready ( function () {
	        // Code
	});
// JQuery Shorthand
	$ ( function () {
	        // Code
	});
// Prototype
document . observe ( 'dom:loaded' , function () {
	        // Code
	});

根据ID获取

// JQuery
$ ( "#idname" );
// Prototype
$ ( "idname" );

根据类名

// JQuery
$ ( ".classname" );
// Prototype
$$ ( '.classname' );

根据第一个符合的类名

// JQuery
$ ( ".classname:first-child" );
// Prototype
$$ ( '.classname' )[ 0 ];

根据ID绑定监听事件

// JQuery
$ ( "#item" ). bind ( 'click' , function () {
	        // Code
	});
	// JQuery Shorthand
	$ ( "#item" ). click ( function () {
	        // Code
	});
// Prototype
$ ( "#item" ). observe ( 'click' , function () {
	        // code
	});

根据符合的类名绑定监听事件

// JQuery
	$(".classname").bind('click',function(){
	 // code
	});
	// JQuery Shorthand
	$ ( ".classname" ). click ( function () {
	        // code
	});
// Prototype
	$$ ( ".classname" ). invoke ( 'observe' , 'click' , function () {
	        // code
	});

结束终止事件

// JQuery
$ ( "#id" ). click ( function () {
	        //code
	        return false ;
	});
// Prototype
$ ( "id" ). observe ( 'click' , function ( e ) {
	        //code
	        Event . stop ( e );
	});

处理观察的元素

// JQuery
$ ( '#idname' ). click ( function () {
	        this . hide (); // Hide the item clicked
	});
// Prototype
$ ( 'idname' ). observe ( 'click' , function ( e ) {
	        el = e . findElement ;
	        el . hide (); // hide the item clicked
	});

根据ID操作类名

// JQuery
	$ ( '#id' ). addClass ( 'red' );
	$ ( '#id' ). removeClass ( 'red' );
// Prototype
	$ ( 'id' ). addClassName ( 'red' );
	$ ( 'id' ). removeClassName ( 'red' );

根据类名操作类名。。

// JQuery
	$ ( '.class' ). addClass ( 'red' );
	$ ( '.class' ). removeClass ( 'red' );
// Prototype
	$$ ( '.class' ). invoke ( 'addClassName' , 'red' );
	$$ ( '.class' ). invoke ( 'removeClassName' , 'red' );

AJAX请求和JSON应用

$.get(url,function(data){
        alert(data . name );
	}, 'JSON' );
// Prototype
new   Ajax . Request ( url ,   {
	        method : 'get' ,
	        onSuccess : function ( transport , json ) {
	                alert ( json . name );
	        }
	});

可以得出结论:jQuery和Prototype大部分是极为相似的,多用几次就都熟了。。

作者: Sjolzy | Google+
地址: http://sjolzy.cn/Difference-between-JQuery-and-Prototype-Summary.html

rails3.2 下 ckeditor 的配置

升级到ruby1.9.3-p429 +rails3.2.13 ,发现原来的 ckeditor 的方式用不了了,Google 到解决方案
打开Gemfile,加入
gem ‘ckeditor’, ‘3.7.3’
如果要上传文件要安装 paperclip
gem ‘paperclip’, ‘~> 3.0’
然后用 bundle install 安装所需的gem
接下来是配置ORM和路由等
执行
rails generate ckeditor:install –orm=active_record –backend=paperclip
在config/applicaiton.rb中加入
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
在config/routes.rb中加入
mount Ckeditor::Engine => ‘/ckeditor’
上面这句也可能已经添加好了,如果没有,就自行添加即可。
在config/environments/production.rb中加入
config.assets.precompile += [‘ckeditor/*’]
然后执行
rake assets:precompile
查看public/assets/ckeditor目录是否存在,里面是否生成css文件和js文件。
在app/views/layouts/application.html.erb 引入helper javascript tag
<%= javascript_include_tag “ckeditor/ckeditor” %>
view页面中加入
<div class=”field”>
<%= cktext_area :post ,:content, :toolbar => ‘Full’, :width => 800, :height => 400 %>
</div>
存入 post#content 的是带有HTML标签的字符串,显示在页面上要用raw 禁止转义
<%= raw @post.content %>
 
源博客 http://virusswb.blog.51cto.com/115214/1048421
在1.9.3-p429 + rails3.2.13 下实测可用