Javascript jQuery .css() 追加或覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4363763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:05:50  来源:igfitidea点击:

jQuery .css() append or overwrite

javascriptjqueryhtmlcss

提问by Darbio

When appending the .css() function in jQuery 1.4.4 will the .css() override each of the preceding css, or will it append it?

在 jQuery 1.4.4 中附加 .css() 函数时, .css() 会覆盖前面的每个 css,还是会附加它?

e.g.

例如

$('.GhostIcon').css('opacity', '0.25').css('filter', 'alpha(opacity=25)').css('-khtml-opacity', '0.25').css('-moz-opacity', '0.25');

And secondly, does this have the same behaviour?

其次,这是否具有相同的行为?

$('.GhostIcon').css('opacity', '0.25');
$('.GhostIcon').css('filter', 'alpha(opacity=25)');
$('.GhostIcon').css('-khtml-opacity', '0.25');
$('.GhostIcon').css('-moz-opacity', '0.25');

回答by Matti Virkkunen

Setting a CSS property on an element will not remove all the other properties it might already have. So yes, it's cumulative. Of course setting a property that's already set will change the value, since you can't have the same property twice.

在元素上设置 CSS 属性不会删除它可能已经拥有的所有其他属性。所以是的,它是累积的。当然,设置一个已经设置的属性会改变值,因为你不能两次拥有相同的属性。

Edit: Also, you only need to set opacity. jQuery will take care of setting things for lesser browsers.

编辑:另外,您只需要设置不透明度。jQuery 将负责为较小的浏览器设置内容。

回答by rahul

These styles are applied so that it will work cross browser way. Also you can append the CSS in a single go

这些样式被应用,以便它可以跨浏览器方式工作。您也可以一次性添加 CSS

$('.GhostIcon').css({'opacity': '0.25', 'filter': 'alpha(opacity=25)', '-khtml-opacity': '0.25', '-moz-opacity': '0.25'}); 

回答by kobe

@JD , if it same value , it will definetly overwrite , if not there it will appened

@JD,如果它的值相同,它肯定会覆盖,如果不存在,它将被附加