zoukankan      html  css  js  c++  java
  • javascript获取style兼容性问题

    获取css 样式的方法有三种 : style, currentStyle , getComputedStyle

    style  (无兼容性问题)

      获取语法: ele.style.attr ; 设置语法:ele.style.attr = '值'

      大多数情况下,javascript中获取和设置style样式都是通过类似" div.style.fontSize" 的方式 , 但是该方式只能获取行内样式 , 即通过link或者<style>引入的样式无法获取, 如行内没设置样式, 返回为空值。 

    currentStyle (支持IE, 不支持FF和Chrome)

      语法 ele.cureentStyle.attr或者 ele.currentStyle[attr]

      当在script中打印 console.log(div.currentStyle)时, 在IE上输出[object Object], 而在Chrome中输出undefined, console.log(window.getComputedStyle)则相反.

    getComputedStyle (支持FF和Chrome,不支持IE)

      语法 window.getComputedStyle(ele, null).attr 或者  window.getComputedStyle(ele, null)[attr]

    解决currentStyle和getComputedStyle的兼容性问题, 注意,两者都只能获取css而不能设置css

     1 function getStyle (ele, styleName) {
     2     if (window.getComputedStyle) {
     3        return window.getComputedStyle(ele, null)[styleName]          
     4     } else {
     5        return ele.currentStyle[styleName]
     6     }
     7 }
     8 
     9 // 调用
    10 getStyle(div, 'fontSize')
  • 相关阅读:
    常见字体图标库——font-awesome
    windows server 2012显示桌面图标
    SE 2014年4月14日
    SE 2014年4月13日
    PPP协议总结
    SE 2014年4月12日
    django运行时报错
    linux-python在vim下的自动补全功能
    python发邮件
    背景透明兼容
  • 原文地址:https://www.cnblogs.com/hughes5135/p/9265734.html
Copyright © 2011-2022 走看看