jquery - 如果主体类发生变化,使用 jQuery 替换 CSS 样式表

标签 jquery css replace stylesheet

我脑子里有一个样式表如下:

<link rel="stylesheet" href="/templates/jooswatch-v3-5/css/bootswatch/superhero/bootstrap.min.css">

如果 body#main.whatever 类发生变化(例如在不同的页面上),我将尝试用以下样式表替换上面的样式表。

改变自: body#main.itemid113

到: body#main.itemid114

<script>
jQuery(document).ready(function() {

   $('body#main.itemid113').replaceWith('<link rel="stylesheet" id="main" href="/templates/jooswatch-v3-5/css/bootswatch/cerulean/bootstrap.min.css" type="text/css" />');

   $('body#main.itemid114').replaceWith('<link rel="stylesheet" id="main" href="/templates/jooswatch-v3-5/css/bootswatch/cosmo/bootstrap.min.css" type="text/css" />');

});
</script>

我该怎么做 - 上面的方法不起作用。

这对吗?

jQuery(function($){
    if ($('.itemid113').length){
        $('body').append("<link rel="stylesheet" href="/templates/jooswatch-v3-5/css/bootswatch/cerulean/bootstrap.min.css" type="text/css" />");
    };
    if ($('.itemid114').length){
        $('body').append("<link rel="stylesheet" href="/templates/jooswatch-v3-5/css/bootswatch/cosmo/bootstrap.min.css");
    }
});

最佳答案

完成此操作的最简单方法是将 id 添加到您的链接标记,即:

<link id="theme" rel="stylesheet" href="/templates/jooswatch-v3-5/css/bootswatch/superhero/bootstrap.min.css">

然后你的脚本变成:

jQuery(function($){
    if ($('.itemid113').length){
        $('#theme').attr("href","/templates/jooswatch-v3-5/css/bootswatch/cerulean/bootstrap.min.css");
    };
    if ($('.itemid114').length){
        $('#theme').attr("href", "/templates/jooswatch-v3-5/css/bootswatch/cosmo/bootstrap.min.css");
    }
});

关于jquery - 如果主体类发生变化,使用 jQuery 替换 CSS 样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27331592/

相关文章:

javascript - API数据提取

javascript - 使表格单元格不会因为内容而垂直扩展

css - 有一个固定位置的 div,如果内容溢出则需要滚动

javascript - 滚动 div 代码

python - 将 [ ] 替换为 raw_input

Java 用另一个子字符串(值)替换子字符串(模式)问题

dictionary - jq 将 value 替换为 map 并根据两个条件在列表中进行选择

javascript - 如何检查bodymovin动画是否完成?

jQuery 函数循环更改背景图像使 Firefox 崩溃

javascript - 如何使用正则表达式取消选中单选按钮?