Simple Method for setting css styles with cssText in js, jscsstextcss styles

Source: Internet
Author: User

Simple Method for setting css styles with cssText in js, jscsstextcss styles

If a tag with the id "no" is displayed on the webpage, tell is used as the div label;

To set the css style of this div in js, it is common:

Var obj = document. getElementByIdx_x_x ('no'); obj. style. width = '400px '; obj. style. height = '300px ';

If you want to set a bunch of css styles, it is too troublesome to put,

Generally, css is used to add className or change className to achieve the desired effect. But if you create an element, do you still want to make it easier? You have to find another solution ~

So we 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 "});

Of course, there is more simple, cssText:

Var obj = document.getelementbyidx_x_x('no'{}obj.style.css Text = "width: 400px; height: 300px ;";

Of course, this method is very easy for the create element to initialize the css style.

The above is all about the simple method for setting css styles with cssText in js provided by xiaobian. I hope it will be helpful to you and support more help ~

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.