JavaScript動態(tài)創(chuàng)建link標簽到head里的方法
更新時間:2014年12月22日 09:02:46 投稿:shichen2014
這篇文章主要介紹了JavaScript動態(tài)創(chuàng)建link標簽到head里的方法,分別介紹了使用jQuery的方法、使用原生javascript方法與IE特有的createStyleSheet方法等,非常具有實用價值,需要的朋友可以參考下
本文實例講述了JavaScript動態(tài)創(chuàng)建link標簽到head里的方法。分享給大家供大家參考。具體分析如下:
相信有很多做前端的朋友碰到過需要用 JavaScript 動態(tài)創(chuàng)建樣式表標簽——link標簽。這里我們就來說說如何在瀏覽器中動態(tài)創(chuàng)建link標簽。
使用 jQuery 創(chuàng)建 link 標簽
如果你開發(fā)中喜歡用jQuery,那么用jQuery在創(chuàng)建link標簽應該是這樣的:
復制代碼 代碼如下:
var cssURL = '/style.css',
linkTag = $('<link href="' + cssURL + '" rel="stylesheet" type="text/css" media="' + (media || "all") + '" charset="'+ charset || "utf-8" +'" />');
// 請看清楚,是動態(tài)將link標簽添加到head里
$($('head')[0]).append(linkTag);
linkTag = $('<link href="' + cssURL + '" rel="stylesheet" type="text/css" media="' + (media || "all") + '" charset="'+ charset || "utf-8" +'" />');
// 請看清楚,是動態(tài)將link標簽添加到head里
$($('head')[0]).append(linkTag);
使用原生 JavaScript 創(chuàng)建 link 標簽
如果你喜歡純天然的 JavaScript,就要需要這么寫:
復制代碼 代碼如下:
var head = document.getElementsByTagName('head')[0],
cssURL = '/style.css',
linkTag = document.createElement('link');
linkTag.id = 'dynamic-style';
linkTag.href = cssURL;
linkTag.setAttribute('rel','stylesheet');
linkTag.setAttribute('media','all');
linkTag.setAttribute('type','text/css');
head.appendChild(linkTag);
cssURL = '/style.css',
linkTag = document.createElement('link');
linkTag.id = 'dynamic-style';
linkTag.href = cssURL;
linkTag.setAttribute('rel','stylesheet');
linkTag.setAttribute('media','all');
linkTag.setAttribute('type','text/css');
head.appendChild(linkTag);
IE 里特有的方法 createStyleSheet
IE 里特有的方法 createStyleSheet 方法也是很方便。
復制代碼 代碼如下:
var head = document.getElementsByTagName('head')[0],
cssURL = 'themes/BlueNight/style.css',
// document.createStyleSheet 的同時就已經把link標簽添加到了head中了,怎么講呢,倒是挺方便
linkTag = document.createStyleSheet(cssURL);
cssURL = 'themes/BlueNight/style.css',
// document.createStyleSheet 的同時就已經把link標簽添加到了head中了,怎么講呢,倒是挺方便
linkTag = document.createStyleSheet(cssURL);
createStyleSheet( [sURL] [, iIndex])方法接受兩個參數,sURL就是CSS文件的URL路徑。iIndex 為可選參數,指插入的link在頁面中stylesheets collection的索引位置,默認是在最后添加新創(chuàng)建的樣式。
完整的解決方案
基本上都介紹完了,來看看完整的解決方案吧:
復制代碼 代碼如下:
function createLink(cssURL,lnkId,charset,media){
var head = $($('head')[0]),
linkTag = null;
if(!cssURL){
return false;
}
linkTag = $('<link href="' + cssURL + '" rel="stylesheet" type="text/css" media="' + (media || "all") + '" charset="'+ charset || "utf-8" +'" />');
head.append(linkTag);
}
function createLink(cssURL,lnkId,charset,media){
var head = document.getElementsByTagName('head')[0],
linkTag = null;
if(!cssURL){
return false;
}
linkTag = document.createElement('link');
linkTag.setAttribute('id',(lnkId || 'dynamic-style'));
linkTag.setAttribute('rel','stylesheet');
linkTag.setAttribute('charset',(charset || 'utf-8'));
linkTag.setAttribute('media',(media||'all'));
linkTag.setAttribute('type','text/css');
linkTag.href = cssURL;
head.appendChild(linkTag);
}
var head = $($('head')[0]),
linkTag = null;
if(!cssURL){
return false;
}
linkTag = $('<link href="' + cssURL + '" rel="stylesheet" type="text/css" media="' + (media || "all") + '" charset="'+ charset || "utf-8" +'" />');
head.append(linkTag);
}
function createLink(cssURL,lnkId,charset,media){
var head = document.getElementsByTagName('head')[0],
linkTag = null;
if(!cssURL){
return false;
}
linkTag = document.createElement('link');
linkTag.setAttribute('id',(lnkId || 'dynamic-style'));
linkTag.setAttribute('rel','stylesheet');
linkTag.setAttribute('charset',(charset || 'utf-8'));
linkTag.setAttribute('media',(media||'all'));
linkTag.setAttribute('type','text/css');
linkTag.href = cssURL;
head.appendChild(linkTag);
}
希望本文所述對大家的javascript程序設計有所幫助。
您可能感興趣的文章:
- JavaScript動態(tài)創(chuàng)建div屬性和樣式示例代碼
- javascript 動態(tài)創(chuàng)建表格
- javascript動態(tài)創(chuàng)建及刪除元素的方法
- JavaScript實現動態(tài)創(chuàng)建CSS樣式規(guī)則方案
- JavaScript與DOM組合動態(tài)創(chuàng)建表格實例
- javascript轉換字符串為dom對象(字符串動態(tài)創(chuàng)建dom)
- javascript 翻頁測試頁(動態(tài)創(chuàng)建標簽并自動翻頁)
- JavaScript 動態(tài)創(chuàng)建VML的方法
- Javascript動態(tài)創(chuàng)建div的方法
- javascript 動態(tài)創(chuàng)建 Option選項

