开发者

How can I set the location of a stylesheet with jQuery?

How can I set the location of 开发者_开发知识库a stylesheet with jQuery?


You can manipulate the <head> element in much the same way that you can elements inside the <body> tag:

$("head>link[href$=mystyle.css]").remove();
$('<link rel="stylesheet" href="/css/anotherstyle.css"/>').appendTo("head");

This removes the mystyle.css <link> tag and insert a new <link> tag with /css/anotherstyle.css at the end of the <head> element.


One approach is to give the <link> element an ID and use that as your selector, changing the href property on the matching element:

$("#myStyle")[0].href = "/path/to/new.css";

Another approach is to have multiple style sheets attached to your document and enable/disable them when necessary:

<link id="style1" href="style1.css" />
<link id="style2" href="style2.css" disabled="disabled" />
$("#style1").attr("disabled", true).next().attr("disabled", false);

This approach is best when regularly switching all the styles for your page.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜