简体   繁体   English

仅在具有特定功能的情况下才使用jquery更改CSS属性值

[英]Change a CSS property value with jquery only when it has a certain function

I want to use jQuery to change a CSS property value but only if the property has a certain function. 我想使用jQuery更改CSS属性值,但前提是该属性具有特定功能。

For example: [class="btn"] { background-image: linear-gradient(red, white) } 例如: [class="btn"] { background-image: linear-gradient(red, white) }

I only want to set background-image to none when it has the function linear-gradient. 我只想在具有线性渐变功能时将background-image设置为none。 I dont want to change it in any other case. 我不想在任何其他情况下更改它。

Thank you very much!!! 非常感谢你!!!

You could get the value for the background-image and then test it against linear-gradient(red, white); 您可以获取background-image的值,然后针对linear-gradient(red, white); :

var element = $('[class=“btn”]'); // or just $('.btn');
if(element.css('background-image') == 'linear-gradient(red, white)') {
    // do something
}

Or maybe just check against linear-gradient so it will match no matter which colors are used by the gradient; 或者也许只是检查linear-gradient以便无论渐变使用哪种颜色,它都将匹配;

var element = $('[class=“btn”]'); // or just $('.btn');
if(element.css('background-image').indexOf('linear-gradient') == 0) { // == 0 to check if the background-image starts with linear-gradient
    // do something
}

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

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