javascript動態(tài)添加樣式(行內(nèi)式/嵌入式/外鏈式等規(guī)則)
更新時間:2013年06月24日 11:17:29 作者:
添加CSS的方式有行內(nèi)式、嵌入式、外鏈式、導(dǎo)入式,下面為大家詳細介紹下javascript動態(tài)添加以上樣式規(guī)則的方法,感興趣的朋友可以參考下哈
添加CSS的方式有行內(nèi)式、嵌入式、外鏈式、導(dǎo)入式
a)動態(tài)引入樣式表文件:
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)嵌入式:
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("函數(shù)最多接收兩個參數(shù)!");
}
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兼容
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)行內(nèi)式:
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")返回[object]
s=ele.style.cssText;
}
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_i;
}
}
ele.style.cssText= styles.join("")+";"+str;
}
}
}
d)導(dǎo)入式:
import "index.css";
操作CSS 類相關(guān)的方法:
var hasClass=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){//存在查找要移除的類
getClass = " "+ele.className+" ".replace(rclass," ");//左右空格,為了使類中各類間均等,方便后面替換
for(var i=0,len=classNames.length; i<len; i++){
getClass = getClass.replace(" "+classNames[i]+" "," ")
}
ele.className=trim(getClass);
}else{//不存在移除所有類
ele.className = "";
}
}
}
}
var toggleClass=function(ele,value){
if(typeof value === "string"){
if(hasClass(ele,value)){
removeClass(ele,value);
}else{
addClass(ele,value);
}
}
}
a)動態(tài)引入樣式表文件:
復(fù)制代碼 代碼如下:
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)嵌入式:
復(fù)制代碼 代碼如下:
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("函數(shù)最多接收兩個參數(shù)!");
}
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兼容
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)行內(nèi)式:
復(fù)制代碼 代碼如下:
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")返回[object]
s=ele.style.cssText;
}
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_i;
}
}
ele.style.cssText= styles.join("")+";"+str;
}
}
}
d)導(dǎo)入式:
import "index.css";
操作CSS 類相關(guān)的方法:
復(fù)制代碼 代碼如下:
var hasClass=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){//存在查找要移除的類
getClass = " "+ele.className+" ".replace(rclass," ");//左右空格,為了使類中各類間均等,方便后面替換
for(var i=0,len=classNames.length; i<len; i++){
getClass = getClass.replace(" "+classNames[i]+" "," ")
}
ele.className=trim(getClass);
}else{//不存在移除所有類
ele.className = "";
}
}
}
}
var toggleClass=function(ele,value){
if(typeof value === "string"){
if(hasClass(ele,value)){
removeClass(ele,value);
}else{
addClass(ele,value);
}
}
}
相關(guān)文章
JS中頁面與頁面之間超鏈接跳轉(zhuǎn)中文亂碼問題的解決辦法
在原頁面一張圖片上添加了一個鏈接,鏈接中有中文,于是在跳轉(zhuǎn)過程中出現(xiàn)中文亂碼問題,下面給大家分享下解決方案2016-12-12JavaScript學(xué)習(xí)筆記之?dāng)?shù)組的增、刪、改、查
這篇文章主要介紹了JavaScript學(xué)習(xí)筆記之?dāng)?shù)組的增、刪、改、查的相關(guān)資料,需要的朋友可以參考下2016-03-03使用bat打開多個cmd窗口執(zhí)行g(shù)ulp、node
本文主要介紹了使用bat打開多個cmd窗口執(zhí)行g(shù)ulp、node的方法。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02