JavaScript Learning Note--csstext

Source: Internet
Author: User

Usually write code, change an element style, you use

Obj.style.width = "200px"; obj.style.position = "absolute"; obj.style.left = "100px";

Code, so that if you change the style a lot of time, you have to write a lot of code, not like jquery, the use of $ (obj). css (...); Are you setting this up?

Then I searched the method of using JavaScript to modify the style in bulk. So see this article, originally there is a csstext attribute, it seems that their basic knowledge is not fully mastered

The following usage was queried in the W3school

It is a text representation of a set of style properties and their values. This text is formatted as a CSS style sheet, removing the curly braces that enclose the element selector for attributes and values.

Setting this property to an illegal value will throw a Domexception exception with code SYNTAX_ERR. When the Css2properties object is read-only, attempting to set this property throws a Domexception exception with code NO_MODIFICATION_ALLOWED_ERR.

Use of Csstext

Obj.csstext = "width:200px;position:absolute;left:100px;";

As mentioned in the article Csstext will erase the previous elements of the style, so you have to use

Obj.csstext + = "width:200px;position:absolute;left:100px;";

But the last semicolon in IE will be deleted.

Obj.csstext + = "; width:200px;position:absolute;left:100px;"; This will solve the problems that arise in IE.

But the code also seemed a little troublesome (at least in my opinion), so I looked for other ways.

Use Jsonfunction SetStyle (Obj,json) {for    (var i in JSON)    {        obj.style[i]=json[i];}    }

When using the direct

SetStyle (obj,{width: ' 200px '; position: ' absolute '; Left: ' 100px ';});

Non-Original

Original: http://rguanghui.sinaapp.com/2013/02/26/javascript_study_note_csstext/

JavaScript Learning Note--csstext

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.