我正在使用 rails 3.2 并且也在使用 coffeescript..
我对在我的应用程序中使用 jquery mobile 有疑问
我有一个像
<a href="#" data-item=12 data-status=true class="follow">
<span class="ui-btn-inner">
<span class="ui-btn-text">
Follow
</span>
</span>
</a>
在我的 CoffeeScript 中,我试图在点击此关注时编写函数,我试图发送一个 ajax 调用。
当我写喜欢
$("a.follow").unbind('click').bind 'click', (event, data) ->
event.stopPropagation()
clickedEl = $(event.target)
console.log(clickedEl)
Console.log 有时会与类“ui-btn-text”一起打印,有时会与“ui-btn-inner”类一起打印。
但是在我的 CoffeeScript 函数中,我实际上需要从标签中获取属性。如何进行此操作
最佳答案
您可以使用 event.currentTarget
而不是 event.target
:
The current DOM element within the event bubbling phase.
演示:http://jsfiddle.net/ambiguous/gdzUC/
或
event.delegateTarget
:The element where the currently-called jQuery event handler was attached.
演示:http://jsfiddle.net/ambiguous/UhBWM/
您也可以使用
closest
:clickedEl = $(this).closest('a')
演示:http://jsfiddle.net/ambiguous/2LHp3/
event.target
值为:The DOM element that initiated the event.
换句话说,它是实际被点击的东西。
currentTarget
和 delegateTarget
将是事件绑定(bind)的东西。使用 closest
这可能是矫枉过正,您通常会使用它来将 DOM 上升到一个容器,该容器包含您正在与之交互的多个项目。
关于jquery-mobile - 在 jquery coffeescript 中绑定(bind)点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10613787/