jQuery 当用户更改主题时,是否可以更改网站图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4875689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible change favicon on site when users change themes?
提问by Preston
i have 2 themes on my site: Red, and Blue. This works fine...
我的网站上有 2 个主题:红色和蓝色。这工作正常...
i must know if i can make something to change the favicon, when users change theme...
当用户更改主题时,我必须知道我是否可以做一些事情来更改网站图标...
i know this code is for implement favicon:
我知道此代码用于实现图标:
<link rel=”shortcut icon” href=”favicon.ico” />
<link rel=”icon” href=”favicon.ico” />
so.. what i need to make this possible? using jQuery?
所以..我需要什么才能使这成为可能?使用jQuery?
i have this script, that i use for change themes:
我有这个脚本,用于更改主题:
$("#painel_faccao li a").click(function() {
$("link#faccao").attr("href",$(this).attr('rel'));
$.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
return false;
});
how i can implement this here??
我怎么能在这里实现这个?
(This question has a more modern approach to this function)
(这个问题对这个功能有更现代的方法)
回答by Todd Moses
There is an answer on here on Stack Overflow: Changing website favicon dynamically
Stack Overflow 上有一个答案:动态更改网站图标
I like this answer best:
我最喜欢这个答案:
If you have the following HTML snippet:
如果您有以下 HTML 代码段:
<link id="favicon" rel="shortcut icon" type="image/png" href="favicon.png" />
You can change the favicon using Javascript by changing the HREF element on this link, for instance (assuming you're using JQuery):
例如,您可以通过更改此链接上的 HREF 元素来使用 Javascript 更改网站图标(假设您使用的是 JQuery):
$("#favicon").attr("href","favicon2.png");
Just use JQuery to check the stylesheet and change the favicon based on it.
只需使用 JQuery 检查样式表并根据它更改网站图标。
回答by user2571141
Yeah, there is another way. Just do it
是的,还有一种方法。去做就对了
- Remove
<link rel="shortcut icon" ...>
- Add the code below
- 消除
<link rel="shortcut icon" ...>
- 添加下面的代码
$(window).load(function () { $('head').append('<link href="your_favicon_name.ico" rel="shortcut icon" type="image/x-icon" />'); });
$(window).load(function () { $('head').append('<link href="your_favicon_name.ico" rel="shortcut icon" type="image/x-icon" />'); });