2015-05-06 69 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非常陌生。

相關問題