简体   繁体   English

通过jQuery修改CSS

[英]Modify css by Jquery

I want to change two properties of a css, One property Value I want to modify using function call and other one is direct property assigning. 我想更改css的两个属性,一个属性值,我想使用函数调用来修改,另一个属性是直接属性分配。 I want to add background image at run time and setting property no repeat as well. 我想在运行时添加背景图像,并设置属性也不要重复。 Following two functions i came to know: 我了解以下两个功能:

$(selector).css(property,function(index,currentvalue)) for changing value by function call and   other one is :-
$(selector).css({property:value, property:value, ...}) for assigning property.

Here I have two properties One is by function call and other one is direct property. 在这里,我有两个属性,一个是通过函数调用,另一个是直接属性。 I tried following code: 我尝试了以下代码:

$('.xl21684').css('background-image', function () {
return 'url(' + imgs[parseInt(this.innerHTML)] + ')'
})
 $('.xl21684').css({background-repeat: no-repeat}) 

But it's not working at all. 但这根本不起作用。

If you would like to go your own way then don't forget to quote the property and value for proper result as @Cerbrus said: 如果您想按照自己的方式行事,请不要忘记引用属性和值以获取正确的结果,如@Cerbrus所说:

('.xl21684').css({'background-repeat': 'no-repeat'});

But you can try just once too: 但是您也可以尝试一次:

$('.xl21684').css('background', function () {
return 'url(' + imgs[parseInt(this.innerHTML)] + ') no-repeat';
});

As per your comment you can use background shorthand property like below: 根据您的评论,您可以使用如下的背景速记属性:

background: [background-color] [background-image] [background-repeat] [background-attachment] [background-position];

The first .css() seems to be fine. 第一个.css()似乎很好。 The second one just needs some quotes: 第二个只是需要一些引号:

$('.xl21684').css({"background-repeat": "no-repeat"});
//                 ^                 ^  ^         ^

That should work. 那应该工作。

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

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