【发布时间】:2026-01-08 01:20:07
【问题描述】:
我正在尝试在咖啡脚本中使用 flot。返回的 javascript 将所有方法包装在函数调用中,因此我无法使用 .bind 事件。 $(this).bind 'plothover', (event, pos, item) -> 当我的鼠标移动时没有被调用
$ ->
$("#flot-placeholder1").text("Amit")
plot = $.plot($("#flot-placeholder1"), dataset, options)
$("#flot-placeholder1").UseTooltip()
$.fn.UseTooltip = ->
alert "UseTooltip"
**$(this).bind 'plothover', (event, pos, item) ->**
alert "UseTooltip"
if item
if (previousLabel isnt item.series.label) or (previousPoint isnt item.dataIndex)
previousPoint = item.dataIndex
previousLabel = item.series.label
$("#tooltip").remove()
x = item.datapoint[0]
y = item.datapoint[1]
color = item.series.color
month = new Date(x).getMonth()
if item.seriesIndex is 0 or item.seriesIndex is 2
showTooltip item.pageX, item.pageY, color, "<strong>" + item.series.label + "</strong><br>" + monthNames[month] + " : <strong>" + y + "</strong>(USD)"
else
showTooltip item.pageX, item.pageY, color, "<strong>" + item.series.label + "</strong><br>" + monthNames[month] + " : <strong>" + y + "</strong>(%)"
else
$("#tooltip").remove()
previousPoint = null
【问题讨论】:
-
封装了哪些方法?生成的 JS 对我来说看起来不错。
-
生成的js "(function() { var options, plot, plotField, previousLabel, previousPoint, showTooltip; $(function() { //somecode }); $.fn.UseTooltip = function( ) { alert("UseTooltip"); return $(this).bind('plothover', function(event, pos, item) { var color, month, x, y; alert("UseTooltip"); if (item) { //somecode }); }; }).call(this); "当我的鼠标在 plot 上时什么都不会触发
-
如果你打开它,它会起作用吗?
-
是的,而不是coffeescript,我直接使用js,没有发生包装,然后它工作正常
-
你试过
coffee --bare编译吗?
标签: javascript jquery ruby-on-rails-3 coffeescript flot