zoukankan      html  css  js  c++  java
  • js原生获取css属性


    原文参考http://blog.csdn.net/lzding/article/details/46317777

    1.写在dom上的属性,内联样式
    <div id="box" style="background-color:#ccc;margin-top:100px;"></div>
    var oBox = document.getElementById('box')
    console.log(oBox.style.width)

    1)对于复合属性(如background),假设行间设置了样式:background-color:#333,不能通过 element.style.background 来获取(见上面例子)
    2)css属性使用驼峰法,如 backgroundColor、marginTop等。


    2.写在css中的属性,非内联样式(chrome)
    var oBox = document.getElementById('box');
    var a = getComputedStyle(oBox, null)['width']; // 100px

    1)对于复合属性:使用 getPropertyValue 获取属性值时,不能使用驼峰写法,如:例子中的 getpropertyValue('backgroundColor') 无法正确获得值,而必须写成 background-color
    2)另外,以下写法也正确:getComputedStyle(oBox, null)['backgroundColor']、getComputedStyle(oBox, null)['background-color'], 以及 getComputedStyle(oBox, null).backgroundColor 等

  • 相关阅读:
    如何利用UltraEdit语法着色来编辑shell脚本
    css 运动背景
    页面加载进度条
    jScrollPane滚动条
    页面加载进度条改进版
    js页面新消息提示
    一道题
    jquery插件 展示信息
    冒泡排序和快速排序
    字体背景
  • 原文地址:https://www.cnblogs.com/victory820/p/7498668.html
Copyright © 2011-2022 走看看