美文网首页
JS中style,currentStyle和getCompute

JS中style,currentStyle和getCompute

作者: 隔壁老王z | 来源:发表于2018-08-17 16:32 被阅读0次

var list = document.getElementById('list')

  • 直接由 list.style.attr (attr代表属性值)获取,这种方式各浏览器都能兼容,但是只能获取行内样式,获取不了外部样式,如果写了行内没有的样式,返回的是空值。
    写法:ele.style.attr(这样为获取),ele.style.attr="值" (这样为设置);
  • currentStyle属性(IE浏览器)和getComputedStyle 属性(谷歌火狐)不能设置属性,只能获取
    语法:
    • IE上兼容:var newLeft = list.currentStyle["left"]
    • 火狐谷歌兼容: var newLeft = window.getComputedStyle(list, null)["left"]
  • 一个兼容的函数:
function getStyle(ele, attr) {
         if (window.getComputedStyle) {
                   return window.getComputedStyle(ele, null)[attr];
                 }
                   return ele.currentStyle[attr];
       }

相关文章

网友评论

      本文标题:JS中style,currentStyle和getCompute

      本文链接:https://www.haomeiwen.com/subject/mlrtiftx.html