jqueryif-then CSS背景更改
基本上我需要一个背景图像来改变,如果另一个是显示。这只是一页多页。这是我目前所拥有的,但它不起作用jqueryif-then CSS背景更改,jquery,Jquery,基本上我需要一个背景图像来改变,如果另一个是显示。这只是一页多页。这是我目前所拥有的,但它不起作用 if ($('#container').css('background') === 'url("/images/container1.png") no-repeat') { $('#container').css('background','url("/images/container2.png") no-repeat'); } 而且我不能在CSS中只为一个页面做这件事 谢谢这不是一个直
if ($('#container').css('background') === 'url("/images/container1.png") no-repeat') {
$('#container').css('background','url("/images/container2.png") no-repeat');
}
而且我不能在CSS中只为一个页面做这件事
谢谢这不是一个直接的答案,但我建议使用类名。直截了当比不断动态更新CSS更容易:
var $myContainer = $('#container');
if ($myContainer.hasClass('background1')) {
$myContainer
.removeClass('background1')
.addClass('background2');
}
尝试使用“背景图像”
if($('#container').css('background-image')='url(/images/container1.png)
您可能还需要添加/删除url中的引号。出于好奇,
console.log($(“#container').css('background'))
show?嗯……是css(“background image”)
还是css(“background url”)呢
返回并设置背景的url?另外,请确保在DOM就绪后调用此函数。与@Kevin Peno的注释一起,为什么不直接使用==
而不是==
?==和===?==之间的区别在于类型更严格,因为类型转换不会发生,因此速度更快-在这种情况下,use of===是有效的,因为只有字符串比较。我不能使用类名,但你是对的。如果它是后台的,那就容易多了。尽管它是CSS文件中的吗?我不这么认为,因为这是由浏览器解析的。
$("#container").each(function(){
var current = $(this);
if( current.css('background').indexOf('some/img/url/img.png') > -1 ) {
current.css('background', 'url(new/img/url/img.png)');
}
});