为什么css后台jQuery代码不工作jQuery设置多个css值

Why css background jQuery code is not working jQuery set multiple CSS values

本文关键字:jQuery css 设置 后台 代码 为什么 工作      更新时间:2023-09-26

我正在尝试使用jQuery添加背景,但此代码不工作

$("#search").css({'background':'url(imageurl) no-repeat center center;width:32px;height:32px;'});

只有下面的代码有效

 $("#search").css({'background':'url(imageurl) '}); 

我想用jquery实现这个

background:url(spinner.gif) no-repeat center center;width:32px;height:32px;

**At least add the reason for downvote..........**

您需要将每个CSS规则放在对象的自己的属性中。试试这个:

$("#search").css({ 
    'background': 'url(imageurl) no-repeat center center',
    'width': '32px',
    'height': '32px'
});

更好的是,将这些规则放入样式表中的一个类中,并使用addClass()代替。这更可取,因为它将CSS逻辑从JS中分离出来。

在下面使用like

$("#search").css({'background': 'url(imageurl) no-repeat center center','background-size':'32px 32px'});

$("#search").css({'background': 'url("imageurl") no-repeat scroll center center / 32px 32px'});