A simple way to set CSS styles with csstext in JS _javascript tips

Source: Internet
Author: User

If you have a label with "No" in your Web page, let's tell it as a DIV tag;

To set the CSS style for this div in JS, it is common practice to:

var obj = document.getelementbyidx_x_x (' no ');
Obj.style.width = ' 400px ';
Obj.style.height = ' 300px ';

If you want to set up a bunch of CSS styles, it's too much trouble to

Under normal circumstances will be combined with CSS to add classname or change classname to achieve the desired effect, but if you create an element, do you want to be so simple? Then we'll think of another way.

So everyone wrote one function after another, the classic two are:

var obj = document.getelementbyidx_x_x (' no ');
function SetStyle (obj, CSS) {for
(var attr in obj) {
obj.style[attr] = css[attr];
}
SetStyle (obj,{width: "400px", Height: "300px"});

There are, of course, simpler, csstext:

var obj = document.getelementbyidx_x_x (' no ');
Obj.style.cssText = "width:400px; height:300px; ";

Of course, this method is simple and convenient for initializing CSS styles for the elements of Create.

The above is small series for everyone to bring the JS with Csstext set CSS style of the whole content of the simple method, hope to help everyone, a lot of support cloud Habitat Community ~

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.