Reading CSS value using JavaScript

前端 未结 7 689
梦毁少年i
梦毁少年i 2021-01-28 17:43

This works:

7条回答
  •  星月不相逢
    2021-01-28 18:17

    Try the following

    window.onload = function(){
    
    var x = document.getElementById("hello");
    var y ="";
    if (x.currentStyle)
        y = x.currentStyle['width'];
    else if (window.getComputedStyle)
        y = document.defaultView.getComputedStyle(x,null).getPropertyValue('width');
    alert(y);
    
    
    
    };
    

    It was inspired from something I read Here

提交回复
热议问题