Javascript CoffeeScript在鼠标上方更改图像src

Javascript CoffeeScript在鼠标上方更改图像src,javascript,ruby-on-rails,coffeescript,Javascript,Ruby On Rails,Coffeescript,我试图在Rails视图中使用CoffeeScript在链接鼠标上更新一个简单的图像src 出于某种原因,这很好: showimghover = (imgpath) -> $('.thelink').mouseover -> imgpath = '../../images/imagefile.jpg' $("#theimage").attr src: '../../images/imagefile.jpg' $('#imghover').css('ba

我试图在Rails视图中使用CoffeeScript在链接鼠标上更新一个简单的图像src

出于某种原因,这很好:

showimghover = (imgpath) ->
  $('.thelink').mouseover ->
    imgpath = '../../images/imagefile.jpg'
    $("#theimage").attr
      src: '../../images/imagefile.jpg'
$('#imghover').css('background','url(../../images/imagefile.jpg)')
但由于某些原因,当我插入字符串变量而不是手动写入URL时,这不会更新img src:

showimghover = (imgpath) ->
  $('.thelink').mouseover ->
    imgpath = '../../images/imagefile.jpg'
    $("#theimage").attr
      src: imgpath
编辑:

为了澄清这一问题,这很好:

showimghover = (imgpath) ->
  $('.thelink').mouseover ->
    imgpath = '../../images/imagefile.jpg'
    $("#theimage").attr
      src: '../../images/imagefile.jpg'
$('#imghover').css('background','url(../../images/imagefile.jpg)')
但当插入变量时,它不起作用:

imgpath = 'url(../../images/imagefile.jpg)'
$('#imghover').css('background', imgpath)

我想我知道了。。。当我将此代码移出showimghover函数时,它会工作。我将不得不用一个函数重新整合这段代码来动态设置图像路径,但我原来的问题已经解决了

$ ->
    $('.style-link').mouseover ->
        $('#imghover').css('display', 'block')
        imgpath = 'url(../../images/OT153575_CRM_FRONT.jpg)'
        $('#imghover').css('background',imgpath)

我仍然不明白为什么会发生这种情况,因为我对CoffeeScript非常陌生。

您能告诉我们如何调用showimghover函数吗?