<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> #div1{height:300px;width:300px;background: green;} </style></head><body><input type="button" value="按钮" id="btn"><div id="div1"></div><script> function getStyle(obj,attr,value){ if(!value){ if(obj.currentStyle){ return obj.currentStyle(attr); }else{ return getComputedStyle(obj,false)[attr]; } }else{ obj.style[attr]=value; } } window.onload=function(){ var oDiv=document.getElementById("div1"); var oBtn=document.getElementById("btn"); oBtn.onclick=function(){ console.log(getStyle(oDiv,"width")); console.log(getStyle(oDiv,"border","1px solid black")); } }</script></body></html>
时间: 2024-10-06 00:42:53