2015-05-06 66 views
0

我想要在Rails视图中使用CoffeeScript在链接mouseover上更新简单图像src。CoffeeScript在鼠标悬停上更改图像src

出于某种原因,这工作得很好:

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

但由于某些原因,当我在字符串变量插入手写的URL,而不是这不会更新IMG SRC:

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

编辑:

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

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

但是,当插入一个变量,它不工作:

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

你能告诉我们你是如何调用showimghover功能的吗? – Mario

回答

0

我想我想通了......当我提出这个代码了它的工作原理“showimghover”功能。我将不得不重新整合这个代码与一个函数动态设置图像路径,但我原来的问题已解决。

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

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

相关问题