jquery coffeescript中的绑定click事件 [英] Binding click event in jquery coffeescript

查看:371
本文介绍了jquery coffeescript中的绑定click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rails 3.2和使用coffeescript太..
我有一个疑问,在我的应用程序中使用jquery mobile
我有一个html像

 < a href =#data-item = 12 data-status = true class =follow> 
< span class =ui-btn-inner>
< span class =ui-btn-text>
关注
< / span>
< / span>
< / a>

在我的coffeescript,我试图写入功能点击这里Follow,我想发送当我写了

  $( a.follow)。unbind('click')。bind'click',(event,data) - > 
event.stopPropagation()
clickedEl = $(event.target)
console.log(clickedEl)

Console.log有时以ui-btn-text类打印span,有时以ui-btn-inner打印。



但是在我的coffeescript函数中,我需要实际从标签获取属性。如何处理此

解决方案

您可以使用 event.currentTarget (而不是 event.target


事件冒泡阶段中的当前DOM元素。


演示: http://jsfiddle.net/ambiguous/gdzUC/



event.delegateTarget


当前调用的jQuery事件处理程序所在的元素。


演示: http://jsfiddle.net/ambiguous/UhBWM/



您也可以使用 最接近

  clickedEl = $(this).closest('a')

演示: http://jsfiddle.net/ambiguous/2LHp3/ p>

event.target value is:


启动事件的DOM元素。


换句话说,它是实际点击的东西。 currentTarget delegateTarget 将是事件绑定到的东西。使用最接近的可能是过度的,你通常使用它来将DOM向上移动到一个容器,其中包含几个要与之交互的项目。


I am working on rails 3.2 and using coffeescript too.. I have a doubt in using jquery mobile inside my application I have a html like

<a href="#" data-item=12 data-status=true class="follow">
  <span class="ui-btn-inner">
    <span class="ui-btn-text">
      Follow
    </span>
  </span>
</a>

In my coffeescript, i am trying to write function on click of this Follow, i am trying to send a ajax call.

when i wrote like

  $("a.follow").unbind('click').bind 'click', (event, data) ->
      event.stopPropagation()
      clickedEl = $(event.target)
      console.log(clickedEl)

Console.log prints span with class "ui-btn-text" at times and span with "ui-btn-inner" at times.

But in my coffeescript function , i need to actually take attributes from a tag. How to proceed with this

解决方案

You can use event.currentTarget instead of event.target:

The current DOM element within the event bubbling phase.

Demo: http://jsfiddle.net/ambiguous/gdzUC/

Or event.delegateTarget:

The element where the currently-called jQuery event handler was attached.

Demo: http://jsfiddle.net/ambiguous/UhBWM/

You could also use closest:

clickedEl = $(this).closest('a')

Demo: http://jsfiddle.net/ambiguous/2LHp3/

The event.target value is:

The DOM element that initiated the event.

In other words, it is the thing that was actually clicked on. The currentTarget and delegateTarget will be the thing that the event is bound to. Using closest is probably overkill for this, you'd usually use that to go up the DOM to an container that holds several items that you're interacting with.

这篇关于jquery coffeescript中的绑定click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆