首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更新主题-不刷新页面的颜色元

更新主题-不刷新页面的颜色元
EN

Stack Overflow用户
提问于 2015-09-01 11:20:57
回答 2查看 2.3K关注 0票数 3

我不确定这是否可能;我们有各种主题颜色(颜色.)每一页都会有变化。我们还使用了许多内容加载,没有页面刷新。

我想使用在铬移动浏览器中可用的主题颜色元标签来设置选项卡颜色。

由于我们的网页不刷新,我想这个颜色反映新的页面背景颜色。我可以删除原来的元,并添加一个新的与以下;

代码语言:javascript
复制
function updateMetaThemeColor(theme) {
    var themeColor;
    if(theme == 'theme-1') {
        themeColor = '#f4dcdc'
    } else if(theme == 'theme-2') {
        themeColor = '#f0e8dd';
    } else if(theme == 'theme-3') {
        themeColor = '#d5e7f0';
    } else if(theme == 'theme-4') {
        themeColor = '#f6efb9';
    } else if(theme == 'theme-5') {
        themeColor = '#eaeae8';
    } else if(theme == 'theme-6') {
        themeColor = '#f8e3c7';
    } else if(theme == 'theme-7') {
        themeColor = '#cde8e8';
    }

    //remove the current meta
    $('meta[name=theme-color]').remove();
    //add the new one
    $('head').append('<meta name="theme-color" content="'+themeColor+'">');
}

这是可行的,因为它移除并重新构造元标记,但正如我所担心的,这种更改没有反映在Chrome移动浏览器上的页面本身。

是否有办法迫使移动浏览器重新查找元标记,并在必要时进行更改?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-08 13:46:09

实际上,上面的方法确实是按照它应有的方式工作的。我想我应该考得更好,嗯。

票数 5
EN

Stack Overflow用户

发布于 2021-11-09 03:05:27

你可以这样做

代码语言:javascript
复制
document.querySelector("meta[name='theme-color']").setAttribute("content", "<your-color>");

代码语言:javascript
复制
document.querySelector("meta[name='theme-color']").content = "<your-color>";
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32330305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档