获取非行间样式
获取非行间样式
原理:
IE: obj.currentStyle[属性名]
谷歌:getComputedStyle(obj, false)[属性名]
代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>获取非行内样式</title> <style> div { width: 300px; height: 200px; background: blue; } </style> <script>
// 获取非行间样式的方法: function getstyle ( obj, val ) { if ( obj.currentStyle ) { return obj.currentStyle[val] } else { return getComputedStyle(obj,false)[val] } } window.onload = function () { var box = document.getElementsByTagName('div')[0]; box.onclick = function () { alert( getstyle(box,'height') ) } } </script> </head> <body> <div></div> </body> </html>