Rails JavaScript 中的 onclick 怎么在 CoffeeScript 中实现?

perish · August 07, 2013 · Last by Perish replied at August 14, 2013 · 2973 hits

<a href="javascript:void(0);" onclick="get_one()">添加</a> 上面的 onclick 要是用 coffeescript 怎么写呢?

node.onclick -> get_one() 
node.onclick(function() {
  return get_one();
});

如果是要定义全局范围可以 @get_on = -> 或者 window.get_on = ->

#1 楼 @metal 恩,谢谢! #2 楼 @rociiu 谢谢,最好不用全局 <a href="javascript:void(0);" onclick="get_one(1)">添加</a> a.onclick -> get_one(id) get_one (id) -> alert id 我试一下说没有 get_one 这个函数, 这里的 id 要怎么传呢?get_one 函数要怎么定义呢?

#3 楼 @Perish

少打了一个=

a.onclick =(id) -> get_one(id)

#4 楼 @metal 这里的 get_one 函数是不是还要重新定义一下! 比如: a.onclick = (id) -> get_one(id)

get_one = (id) -> alert id

#6 楼 @metal 除了这个还有什么其他学习资料吗?

呵呵,最近也在学习 coffee,觉得还不错~越来越喜欢 RUBY 了~

#10 楼 @suchiva 呵呵,一块学习啊!

#8 楼 @metal 我在一个页面加载完之后要重新编辑一个数据,通过 js 得到这个数据,并且编辑然后通过 js 也提交了,通过 render 然后显示了这个更新的数据,但是在点击编辑的时候却不能通过 js 编辑了?

#12 楼 @Perish 用 jQuery 绑成 delegate 或者重新绑一次就好了

#13 楼 @jiyinyiyong 恩,我在瞧瞧!谢谢

perish closed this topic. 14 Jul 16:50
You need to Sign in before reply, if you don't have an account, please Sign up first.