Get the last style of the DOM element in memory to modify the Currentstyle method under obj

Source: Internet
Author: User

In the DOM operation on the page in the <style></style> style of operation, found invalid, I think it is because the page Dom parsing the label's style content will be read into memory, so JS operation can not get the contents of this tag

The style of a tag may be defined by more than one style file, so the browser in the interpretation of the label will have a way to calculate the last style of the label, want to change the style of the label in <style> defined format must go to the last browser computer computed style, Here, using the Currentstyle method under obj, the specific test is as follows:

"Text/css">div{width:300px;        height:300px; Border-bottom:1px solid black;    }. test1{background:red;    }. test2{Background:green; }</style><script type="Text/javascript">function GetStyle (obj,attr) {returnObj.currentstyle? OBJ.CURRENTSTYLE[ATTR]: getComputedStyle (obj,NULL) [attr]; //return getComputedStyle (obj,null) [attr];} function T () {varm = document.getElementsByTagName ('Div')[0];        Console.log (m);        Console.log (M.classname); if(!m.classname.indexof ('test1') ) {M.classname='test2'; //alert (m.style.width);        }Else{m.classname='test1'; }            //alert (GetStyle (M, ' width ')); //return;GetStyle (M,'width') = parseint (GetStyle (M,'width')) +5; M.style.height= parseint (GetStyle (M,'Height')) +5; M.style.borderbottom= parseint (GetStyle (M,'BorderBottom')) +1; }</script><divclass="test1"onclick="t ();">It's a special effect .</div>

The ternary operator is used because IE6 only

getComputedStyle (obj,null) [attr]

This allows you to manipulate the style.

Get the last style of the DOM element in memory to modify the Currentstyle method under obj

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.