3 回答

TA貢獻1874條經驗 獲得超12個贊
IE處理完頁面加載的所有樣式后,添加另一種樣式表的唯一可靠方法是 document.createStyleSheet(url)
有關更多詳細信息,請參見createStyleSheet上的MSDN文章。
url = 'style.css';
if (document.createStyleSheet)
{
document.createStyleSheet(url);
}
else
{
$('<link rel="stylesheet" type="text/css" href="' + url + '" />').appendTo('head');
}

TA貢獻1866條經驗 獲得超5個贊
僅在鏈接元素附加到頭部之后,才需要最后設置href屬性:
$('<link>')
.appendTo('head')
.attr({type : 'text/css', rel : 'stylesheet'})
.attr('href', '/css/your_css_file.css');

TA貢獻1776條經驗 獲得超12個贊
這似乎在IE中也有效:
var link = document.createElement('link');
link.rel ='樣式表';
link.type ='text / css';
link.href ='/includes/style.css';
document.getElementsByTagName('head')[0] .appendChild(link);
添加回答
舉報