<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title></title> <style> * {margin:0;padding:0; }Div{width:300px;Height:200px;Background-color:Pink; Left:100px; } </style></Head><Body><inputtype= "button"ID= "BT"value= "Show effect"/><DivID= "DV"></Div><Scriptsrc= "Common.js"></Script><Script>my$ ("BT"). onclick= function () { //gets the value of the left position of the element//Console.log (my$ ("DV"). offsetleft);//Cannot get //Google, Firefox supportConsole.log (window.getComputedStyle (my$ ("DV"), NULL). left); //IE support//Console.log (my$ ("DV"). Currentstyle.left); }; //get any attribute value for any element functionGetStyle (element, attr) {//determine if the browser supports this method if(window.getComputedStyle) {returnwindow.getComputedStyle (element,NULL) [attr]; } Else { returnElement.currentstyle[attr]; } } //Test//GetStyle (my$ ("DV"), "left");</Script></Body></HTML>
JavaScript gets element style attributes and compatible code encapsulation