JavaScript get Styles

前端 未结 3 1948
心在旅途
心在旅途 2020-11-30 12:12

Is it possible to get ALL of the styles for an object using JavaScript? Something like:


main.css
-------
#myLayer {
  position: absolute;
  width: 200px;
          


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 12:33

    You might use:

    var ob = document.getElementById("myLayer");
    var pos = ob.style.position;
    

    Every CSS property has it's own object model. Usually those css properties that contain '-' are written using java model.

    For example:

    //getting background-color property
    var ob = document.getElementById("myLayer");
    var color = ob.style.backgroundColor;
    

    If you want to get all the css properties that are defined for an object, you will have to list them one by one, because even if you did not set the property in your style sheet, an object will have it with the default value.

提交回复
热议问题