简体   繁体   English

背景不变

[英]Background doesn't change

Good day. 美好的一天。

HTML HTML

<div id="Button"></div>

CSS CSS

background: url("../images/play.png") left center no-repeat;
background-size: cover;
width: 100px;
height: 100px;
float: left;
cursor: pointer;

JQuery JQuery的

$("#Button").on("click",function() {
  $(this).css("background", 'url("../images/pause.png") left center no-repeat;');
});

But unfortunately, the background doesn't change. 但是不幸的是,背景并没有改变。 Why doesnt it? 为什么不呢?

Your issue is with the semicolumn in the end of the property value. 您的问题是属性值末尾的分号。 remove it and try. 删除并尝试。

EX: EX:

$(this).css("background", 'url("http://placehold.it/32x32") left center no-repeat');

Fiddle 小提琴

presence of semi-column while specifying the value for the css property name makes it invalid and it doesn't get applied. 在为css属性名称指定值时,如果存在半列,则会使其无效并且不会被应用。

 $(this).css("background", 'url("../images/pause.png") left center no-repeat;');
                                                                            ^___ Here

Also do note that applying css directly to the element makes it more difficult for cascading the styles and less maintainable, since they are applied directly on to the style property of the element. 还要注意,直接将css应用于元素会使级联样式更加困难且难以维护,因为它们直接应用于元素的style属性。 Best way to go is to add a css rule and set the class to the element. 最好的方法是添加css规则并将类设置为元素。

This is more efficient way and you seperate your code 这是更有效的方式,您可以分隔代码

HTML HTML

<div id="Button"></div>

CSS CSS

background: url("../images/play.png") left center no-repeat;
background-size: cover;
width: 100px;
height: 100px;
float: left;
cursor: pointer;

div with Class bg div类别bg

div.bg { div.bg {

background: url("../images/pause.png") left center no-repeat;

} }

JQuery JQuery的

$("#Button").on("click",function() {
  $(this).addClass("bg");
});

Try to set each property separately. 尝试分别设置每个属性。 Also you should check that url is correct. 另外,您应该检查网址是否正确。

Try this: 尝试这个:

$("#Button").on("click", function () {
    $(this).css({
        'background-image': 'url("../images/pause.png")', 
        'background-position': 'left center', 
        'background-repeat': 'no-repeat'
    });
});

As Bergi and André Dion mentioned in the comments, you can remove the background-position and background-repeat properties if they don't need to change when the background image does. 正如Bergi和AndréDion在评论中提到的那样,如果背景图像需要更改时不需要更改background-positionbackground-repeat属性,则可以删除它们。

The issue may have been incorrect image paths or the semicolon in the css (adding a semicolon will make it not work). 问题可能是图像路径不正确或CSS中的分号(添加分号将使其无法使用)。 I have created a jsfiddle the demonstrates the solution to your problem, although with alternate images since I don't have access to your originals. 我创建了一个jsfiddle演示了您的问题的解决方案,尽管由于无法访问您的原始照片,所以使用了替代图像。

http://jsfiddle.net/VpWcB/ http://jsfiddle.net/VpWcB/

The relevant js code is reproduced below: 相关的js代码复制如下:

$("#Button").on("click", function(){
    $(this).css('background', 'url(../images/pause.png) left center no-repeat');
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM