[英]jQuery inline style rgb fallback for rgba color IE 8
I am using jQuery to set the background of a div and i want to use rgba color in the browsers that support it but if they don't i would like to use rgb color as a fallback. 我正在使用jQuery设置div的背景,我想在支持它的浏览器中使用rgba颜色,但如果不这样做,我想使用rgb颜色作为后备。 I know how to achive this with plain css. 我知道如何用普通的CSS实现这一点。
style="background: rgb(250, 0, 0); background: rgba(250, 0, 0, 0.5);"
Do you know how i could achieve this with jQuery .css()
? 你知道我如何用jQuery .css()
吗?
I believe if you use css()
, it will overwrite the previous value for a property. 我相信,如果您使用css()
,它将覆盖属性的先前值。 If you want to do something like that, you can either set it manually via attr()
, or you can use the callback function to see if it's IE or not, and set the value accordingly. 如果要执行类似的操作,则可以通过attr()
手动设置它,也可以使用回调函数查看它是否为IE,并相应地设置值。
using attr()
使用attr()
$('element').attr('style','background: rgb(250, 0, 0); background: rgba(250, 0, 0, 0.5);');
using .css()
使用.css()
$('element').css('background', function() {
// if you're using jquery version < 1.9
if($.browser.msie && parseInt($.browser.version, 10) === 8)
return 'rgb(250, 0, 0);';
else
return "rgba(250, 0, 0, 0.5)";
});
NOTE: if you're using jquery version 1.9 or above, $.browser
is removed, so you will need to detect IE8 in a different way. 注意:如果您使用的是jQuery 1.9或更高版本,则$.browser
被删除,因此您将需要以其他方式检测IE8。
您必须通过CSS两个参数
$(this).css("background-color", "rgb(255, 255, 255)");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.