1IE与DOM下访问内联样式的常用方法

1ie下访问内联样式的方法和style的两个常用属性
1this.style.cssText
<div
onclick="alert(this.style.cssText)"></div>
//outputs background-color: red; height: 50px; width: 50px

2每个元素都有一个currentStyle用以反映当前style属性。
oDiv.currentStyle.backgroundColor

至于ie访问内敛样式只需写Element.style.propertyValue

2DOM下访问内联样式的常用方法.(firefox)

通过Element.style访问如下方法。
1getPropertyValue(name) 返回样式值
2getPropertyPriority() 若指定important,则返回important,否则返回空。
3item(index) 返回给定的目标索引处的css属性名称
4removePropertyPriority(name) 移除指定样式。
5setProperty(propertyName,value,priority) 设置属性,最后一个为空/important

<html>
    
<head>
        
<title>Style Example</title>
        
<script type="text/javascript">
            
function useMethods() {
                
var oDiv = document.getElementById("div1");
                alert(oDiv.style.item(
0));    //outputs "background-color"
                alert(oDiv.style.getPropertyValue("background-color")); 
                oDiv.style.removeProperty(
"background-color");
    
            }
        
</script>
    
</head>
    
<body>
        
<div id="div1" style="background-color: red; height: 50px; width: 50px"  onmouseover="this.style.setProperty('background-color', 'blue', '')"
             onmouseout
="this.style.setProperty('background-color', 'red', '')"></div><br />
        
<input type="button" value="Use Methods" onclick="useMethods()" />
    
</body>
</html>

(提示:仅firefox下可以运行,IE不支持这些方法)

3IE与DOM访问外部样式的方法及常用属性
看个例子吧,里面针对IE和DOM的不同,及各个属性的使用我已经在注释里写明了。

IE与DOM下访问内联样式和外部样式表的常用方法总结<html>
IE与DOM下访问内联样式和外部样式表的常用方法总结    
<head>
>

这里要注意的是在javascript里改变的样式,实际并没有改变样式表里的属性,相当于重写了外部属性
所以用oRules[0].style.backgroundColor访问样式表的时候,其值并没有改变改变的只是当前的外部样式的属性。oDiv.style.backgroundColor

4最后举一个用javascript动态给元素批量增加style的小例子。


提示:可以先在文本框内,根据需要修改代码后再运行

相关文章:

  • 2021-04-09
  • 2021-12-10
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
猜你喜欢
  • 2021-12-10
  • 2021-12-10
  • 2021-12-10
  • 2022-12-23
  • 2021-12-10
  • 2021-12-07
  • 2021-11-05
相关资源
相似解决方案