【发布时间】:2014-04-29 18:02:19
【问题描述】:
我正在尝试使用 jquery 从渐变背景切换到元素的纯背景。
由于某些原因,我不能使用 toggleClass 和其他类方法,所以我必须修改元素的 css 属性——在我的例子中是背景颜色。
问题是,当我尝试接收当前背景 css 属性时,.css() 方法返回一些奇怪的东西。
我的元素具有多个背景(渐变)属性,针对不同的浏览器进行了优化
.element {
background: #ce4f57 !important;
background: -moz-linear-gradient(top, #ce4f57 0%, #b7333b 100%) !important;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ce4f57), color-stop(100%, #b7333b)) !important;
background: -webkit-linear-gradient(top, #ce4f57 0%, #b7333b 100%) !important;
background: -o-linear-gradient(top, #ce4f57 0%, #b7333b 100%) !important;
background: -ms-linear-gradient(top, #ce4f57 0%, #b7333b 100%) !important;
background: linear-gradient(to bottom, #ce4f57 0%, #b7333b 100%) !important;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ce4f57', endColorstr='#b7333b', GradientType=0) !important;
}
当我尝试接收该属性时
$('.element').css('background');
我明白了:
rgba(0, 0, 0, 0) linear-gradient(rgb(206, 79, 87) 0%, rgb(183, 51, 59) 100%) repeat scroll 0% 0% / auto padding-box border-box
据我了解,它是编译属性?我可以通过 js 函数获取原始 css 吗? 如果不是,请建议如何编写正则表达式来获得渐变的第一种颜色,假设在不同的浏览器中可能有不同的编译 css...
【问题讨论】:
-
我说它返回您的特定浏览器当前正在使用的值..
-
看起来像这样,但我需要第一种颜色才能将背景从渐变变为纯色,我该如何获得它?
-
您的意思是您正在寻找第一个值,也就是。背景:#ce4f57!重要; ?
-
为什么不能使用toggleClass等类方法?有什么限制?
-
因为它应该是不同类的一个功能......许多不同的类
标签: jquery css background