Javascript 如何动态更改CSS样式?
我有一个简单的元素:Javascript 如何动态更改CSS样式?,javascript,css,Javascript,Css,我有一个简单的元素: 如何仅动态更改nav index和nav up document.getElementById('a1').style.navIndex = newIndex; document.getElementById('a1').style.navUp = newValue; 请注意,根据和,nav index和nav up仅由Opera支持 请注意,根据和,nav index和nav up仅由Opera支持。如下: var a1 = document.getElementB
如何仅动态更改nav index
和nav up
document.getElementById('a1').style.navIndex = newIndex;
document.getElementById('a1').style.navUp = newValue;
请注意,根据和,nav index
和nav up
仅由Opera支持
请注意,根据和,nav index
和nav up
仅由Opera支持。如下:
var a1 = document.getElementById('a1');
a1.style['nav-index'] = /* somevalue */;
a1.style['nav-up'] = /* somevalue */;
// or (style properties with hypens must be converted
// to a camel case representation for use in 'dot notation')
a1.style.navIndex = /* somevalue */;
a1.style.navUp = /* somevalue */;
// and to retrieve the original values:
a1.style['nav-index'] = '';
a1.style['nav-up'] = '';
此外,建议将内联样式移动到css文件中的类
.a1nav {
position:absolute;
top:0;
left:0;
nav-index:1;
nav-down:#b1;
nav-right:#a3;
nav-left:#a1;
nav-up:#a1;
}
像这样:
var a1 = document.getElementById('a1');
a1.style['nav-index'] = /* somevalue */;
a1.style['nav-up'] = /* somevalue */;
// or (style properties with hypens must be converted
// to a camel case representation for use in 'dot notation')
a1.style.navIndex = /* somevalue */;
a1.style.navUp = /* somevalue */;
// and to retrieve the original values:
a1.style['nav-index'] = '';
a1.style['nav-up'] = '';
此外,建议将内联样式移动到css文件中的类
.a1nav {
position:absolute;
top:0;
left:0;
nav-index:1;
nav-down:#b1;
nav-right:#a3;
nav-left:#a1;
nav-up:#a1;
}
创建一个新的css并像这样调用它们
function changeClass (elementID, newClass) {
var element = document.getElementById(elementID);
element.setAttribute("class", newClass); //For Most Browsers
element.setAttribute("className", newClass); //For IE; harmless to other browsers.
}
创建一个新的css并像这样调用它们
function changeClass (elementID, newClass) {
var element = document.getElementById(elementID);
element.setAttribute("class", newClass); //For Most Browsers
element.setAttribute("className", newClass); //For IE; harmless to other browsers.
}
您可以使用jquery更改导航索引和导航向上
$('a#a1').attr('nav-index', '2'); //If you want to change it to 2.
$('a#a1').attr('nav-up', '#5'); //If you want to change it to #5.
您可以使用jquery更改导航索引和导航向上
$('a#a1').attr('nav-index', '2'); //If you want to change it to 2.
$('a#a1').attr('nav-up', '#5'); //If you want to change it to #5.
是否可以将这些内联样式移动到单独的样式表?是否可以将这些内联样式移动到单独的样式表?