2013-07-30 85 views
0

可能是什麼問題?控制檯告訴我,變量y包含現有外部圖像的右側絕對路徑(啓用了直接鏈接)。背景設置字體顏色變爲紅色後。jQuery set backround image not working

console.log("image url: "+y); 
     $(this).css("background","url('"+y+"') !important;"); 
     $(this).css("color","red"); 

它不適用於背景圖像。

+2

看來,如果你刪除'重要的工作;!'... – gvee

+0

把那作爲一個答案@gvee – putvande

+0

是的,它的工作原理。謝謝! –

回答

0

你試過嗎?

$(this).css({ 
    backgroundImage:"url('"+y+"') !important" 
    }); 

我建議當你通過多個CSS樣式相同的元件使用對象而使用的CSS命令多次:

$(this).css({ 
     backgroundImage:"url('"+y+"') !important", 
     color: "red" 
     }); 
+0

是的,它不工作。 –

0

嘗試

$(this).css("background","url('" + y + "') !important"); 
0

我會放它在一個單一的電話給css像:

$(this).css({"background":"url('"+y+"') !important", "color":"red"}); 
+0

是的,我即將這樣做,但需要在以這種方式優化代碼之前修復一些問題。 –

0

正如其他人所說,這是更好地傳遞一個對象,而不是調用CSS功能幾次。另外,在我看來,如果你不使用css shorthands更清晰。

無論如何,試試在URL中使用雙引號。

$(this).css({'background-image':'url("'+y+'") !important', 'color':'red'});