2012-06-24 33 views
0

CSS渐变描述here,但我不知道如何在JavaScript中选择这些属性。如果可能的话,我宁愿不使用jQuery。我该如何去设置JavaScript中的CSS渐变背景?

编辑:只是执行以下操作似乎并没有工作...

document.getElementById("selected-tab").style.background = "#860432"; 
document.getElementById("selected-tab").style.background = "-moz-linear-gradient(#b8042f, #860432)"; 
document.getElementById("selected-tab").style.background = "-o-linear-gradient(#b8042f, #860432)"; 
document.getElementById("selected-tab").style.background = "-webkit-gradient(linear, 0% 0%, 0% 100%, from(#b8042f), to(#860432))"; 
document.getElementById("selected-tab").style.background = "-webkit-linear-gradient(#b8042f, #860432)"; 
+0

似乎工作:HTTP: //jsfiddle.net/B6sEr/(Chrome 19,Firefox 13)。 –

+0

对于普通的JavaScript解决方案,请参阅:http://stackoverflow.com/a/15071347/1306809 –

回答

1

我不是JS专家,但我的猜测是,你的设置将覆盖对方,所以你可能要创建此类似.gradientBackground一个CSS类选择,并检查了下面的链接:

Change an element's class with JavaScript

+0

如果您查看了描述渐变效果的页面,您会意识到CSS会创建渐变效果,背景效果,必须重载才能跨浏览器兼容。 – Dan

+1

我知道如何CSS渐变的作品,但覆盖不会超载... – Aznim

0

如果你不想使用jQuery你想如何应用的样式?您应该创建一个包含每个浏览器渐变的类或ID。然后使用jQuery的那类设置为ID “选择标签”

.someGradient{ 
    background: #1e5799; /* Old browsers */ 
    background: -moz-linear-gradient(top, #1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%); /* FF3.6+ */ 
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(50%,#2989d8), color-stop(51%,#207cca), color-stop(100%,#7db9e8)); /* Chrome,Safari4+ */ 
    background: -webkit-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* Chrome10+,Safari5.1+ */ 
    background: -o-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* Opera 11.10+ */ 
    background: -ms-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* IE10+ */ 
    background: linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* W3C */ 
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0); /* IE6-9 */ 
} 

jQuery的

$("#selected-tab").addClass('someGradient'); 

之前

<div id='selected-tab' class='arial'>Hello world</div> 

<div id='selected-tab' class='arial someGradient'>Hello world</div> 
+1

有人要求,如果可能的话,答案不应该涉及jQuery。 – Aznim

+2

然后使用Javascript'document.getElementById(“selected-tab”)。className + =“MyClass”;' – Anagio