How to dynamically add style rules in Javascript depth

Source: Internet
Author: User
You can add CSS in the following ways: inline, embedded, external chained, and imported. A. dynamically introduces the style sheet file: function loadlink (URL) {var link = document. createelement ("Link"); Link. type = "text/CSS"; Link. rel = "stylesheet"; Link. href = URL; var head = document. getelmentsbytagname ("head") [0]; head. appendchild (Link);} B) embedded: function insertstyles () {var doc, csscode = [], csstext; var Len = arguments. length; var head, style, firststyle; If (LEN = 1) {Doc = Document; csscode. push (arguments [0 ])} Else if (LEN = 2) {Doc = arguments [0]; csscode. push (arguments [1]);} else {alert ("the function can receive up to two parameters! ");} Head = Doc. getelementsbytagname ("head") [0]; styles = head. getelementsbytagname ("style"); If (styles. length = 0) {If (Doc. createstylesheet) {// iedoc. createstylesheet ();} else {// ffvar tempstyle = Doc. createelement ("style"); tempstyle. setattibute ("type", text/CSS "); head. appendchild (tempstyle) ;}} firststyle = styles [0]; csstext = csscode. join ("\ n"); If (! + "\ V1") {// opacity compatible with var str = csstext. Match (/opacity :( \ D? \. \ D +);/); If (STR! = NULL) {csstext = csstext. replace (STR [0], "filter: alpha (opacity =" + parefloat (STR [1]) * 100 + ")" plain text + = csstext ;} else if (Doc. getboxobjectfor) {firststyle. innerhtml + = csstext;} else {firststyle. appendchild (Doc. createtextnode (csstext);} C) in-row formula: var addstyle = function (Ele, STR) {var S = ELE. getattribute ("style"), styles; If (STR & typeof STR = "string") {If (! Ssf-cancele.style.css text = STR;} else {If (S = '[ object] ') {// IE6/7 E. getattribute ("style" returns [objectobject]s=ele.style.css text;} styles = trim (s ). split (";"); For (VAR I = 0, Len = styles. length; I <Len; I ++) {var style_ I = trim (styles [I]); var ATTR = style_ I .split (":") [0]; If (Str. indexof (ATTR)>-1) {styles [I] = "" ;}else{styles[ I ]=style_ile}}ele.style.css text = styles. join ("") + ";" + STR ;}} d) Import: Import "index.css"; methods related to CSS operations: var hasc Lass = function (Ele, value) {var rclass =/[\ n \ t \ r]/g, value = "" + value + ""; Return (Ele. nodetype = 1) & ("" + ELE. classname + ""). replace (rclass ,""). indexof (value)>-1;} var trim = function (VAL) {return Val. replace (/(^ \ s +) | (\ s + $)/g, "");} var addclass = function (Ele, value) {var rspace =/\ s +/, classnames, getclass; If (Value & typeof value = "string") {classnames = value. split (rspace); If (Ele. nodetype = 1) {If (! Ele. classname & classnames. length = 1) {ELE. classname = value;} else {getclass = "" + ELE. classname + ""; for (VAR I = 0, Len = classnames. length; I <Len; I ++) {var cname = classnames [I]; If (! Hasclass (Ele, cname) {getclass + = cname + "" ;}} ELE. classname = trim (getclass) ;}}} var removeclass = function (Ele, value) {var rclass =/[\ n \ t \ r]/g, classnames, getclass; if (Value & typeof value = "string") | value = undefined) {classnames = (value | ""). split (rspace); If (Ele. nodetype = 1 & ELE. classname) {If (value) {// There is a search for the class to be removed getclass = "" + ELE. classname + "". replace (rclass, ""); // left and right spaces. To ensure equality among classes, replace for (VAR I = 0, Len = classnames. length; I <Len; I ++) {getclass = getclass. replace ("" + classnames [I] + "", "")} ELE. classname = trim (getclass);} else {// remove all classes ELE. classname = "" ;}}} var toggleclass = function (Ele, value) {If (typeof value = "string") {If (hasclass (Ele, value )) {removeclass (Ele, value) ;}else {addclass (Ele, value );}}}

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.