JS-DOM:获取计算后的样式(同时兼容IE跟其他浏览器)

时间:2014-08-07 18:43:40   收藏:0   阅读:415

HTML部分

<div id="div1" style="">打发第三方</div>

CSS部分

<style type="text/css">
#div1{
    height: 100px;
    width: 100px;
    font-size: 12px;
    background-color: #ccc;
}
</style>

JS部分

方法一:

<script>

window.onload=function(){

  var oDiv=document.getElementById("div1");

  oDiv.onclick=function(){

    if(window.getComputedStyle){

      alert(getComputedStyle(oDiv, null).fontSize);

    }

    else{

      alert(oDiv.currentStyle.fontSize);  

    }

  }

}

</script>

方法二:

<script>

function getStyle(obj,sStyle){

  if(window.getConputedStyle){

    return getComputedStyle(obj,null)[sStyle];

  }

  else{

    return obj.currentStyle[sStyle];

  }

}

window.onload=function(){

  var oDiv=document.getElementById("div1");

  oDiv.onclick=function(){

    alert(getStyle(oDiv,"fontSize"));

  }

}

</script>

 

JS-DOM:获取计算后的样式(同时兼容IE跟其他浏览器),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!