2013-07-30 34 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'});