开发者

Adding External Stylesheet using JavaScript

I need to add an link to an external css file in my header using an external javascript file. Don't ask why; I just need to do it. document.writ开发者_Go百科e() doesn't work btw.


var element = document.createElement("link");
element.setAttribute("rel", "stylesheet");
element.setAttribute("type", "text/css");
element.setAttribute("href", "external.css");
document.getElementsByTagName("head")[0].appendChild(element);


Alternative one liner:

document.head.insertAdjacentHTML( 'beforeend', '<link rel=stylesheet href=/ext.css>' );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜