JS set multiple CSS Styles

Source: Internet
Author: User

If you add a CSS style to an element in a Web page that id= "mydiv", get the node first:
var obj = document.getElementById ("mydiv");
To add CSS to a node:
If you need to add a few CSS, you can obj.style.width = "300px";
If you need to add more than one CSS statement, this will be more than one, such as:
Obj.style.width= "300px";
Obj.style.height= "300px";
Obj.style.border= "1px solid #ddd";
......
This will be a bit of a hassle, and you need to repeatedly write similar statements, then you can:
(1) Set CSS by writing JS function yourself
function Setcss (OBJ,CSS) {
for (var attr in CSS) {
OBJ.STYLE[ATTR] = css[attr];
}
}
Setcss (obj,{"width": "300px", "height": "300px", "Border": "1px solid #ddd"}});
(2) Setting CSS through the Csstext method
Obj.style.cssText = "width:300px;height:300px;border:1px solid #ddd;";
This will be simple and convenient, especially for the new Create element to initialize the CSS style

JS set multiple CSS Styles

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.