Home > Web Front-end > JS Tutorial > JavaScript dynamically adds styles (inline/embedded/external link, etc. rules)_javascript skills

JavaScript dynamically adds styles (inline/embedded/external link, etc. rules)_javascript skills

WBOY
Release: 2016-05-16 17:31:31
Original
2503 people have browsed it

The ways to add CSS include inline, embedded, external link, and imported
a) Dynamically introduce style sheet files:

Copy code The code is as follows:

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:
Copy code The code is as follows:

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 accepts at most two parameters!");
}
head = doc.getElementsByTagName("head")[0] ;
styles= head.getElementsByTagName("style");
if(styles.length == 0){
if(doc.createStyleSheet){//ie
doc.createStyleSheet();
}else{//FF
var tempStyle = doc.createElement("style");
tempStyle.setAttibute("type","text/css");
head.appendChild(tempStyle );
}
}
firstStyle = styles[0];
cssText=cssCode.join("n");
if(! "v1"){//opacity compatible
var str = cssText.match(/opacity:(d?.d );/);
if(str!=null){
cssText = cssText.replace(str[0],"filter: alpha(opacity=" pareFloat(str[1])*100 ")");
}
}
if(firstStyle.styleSheet){
firstStyle.styleSheee.cssText = cssText;
}else if(doc.getBoxObjectFor){
firstStyle.innerHTML = cssText;
}else{
firstStyle.appendChild(doc.createTextNode(cssText));
}
}

c) Inline:
Copy the code The code is as follows:

var addStyle=function (ele,str){
var s = ele.getAttribute("style"),styles;
if(str && typeof str === "string"){
if(! s){
ele.style.cssText = str;
}else{
if(s == '[object]'){//IE6/7 e.getAttribute("style") returns [ object]
s=ele.style.cssText;
}
styles= trim(s).split(";");
for (var i=0,len=styles.length; ivar style_i=trim(styles[i]);
var attr=style_i.split(":")[0];
if(str.indexOf(attr) >-1){
styles[i]="";
}else{
styles[i]=style_i;
}
}
ele.style.cssText= styles.join("") ";" str;
}
}
}

d) Import:
import "index.css";
Methods related to operating CSS classes:
Copy code The code is as follows:

var hasClass=function (ele,value){
var rclass = /[ntr]/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; ivar cname=classNames[i];
if(!hasClass(ele,cname)){
getClass = cname " ";
}
}
ele.className = trim(getClass);
}
}
}
}
var removeClass=function(ele,value){
var rclass = /[ntr]/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 class to be removed
getClass = " " ele.className " ".replace(rclass," ");/ / Spaces left and right, in order to make the categories in the class equal, facilitate subsequent replacement
for(var i=0,len=classNames.length; igetClass = getClass.replace(" " classNames[i] " "," ")
}
ele.className=trim(getClass);
}else{//Remove all classes if they do not exist
ele.className = "";
}
}
}
}
var toggleClass=function(ele,value){
if(typeof value === "string"){
if(hasClass (ele,value)){
removeClass(ele,value);
}else{
addClass(ele,value);
}
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template