2013-10-05 63 views

回答

53

小提琴:http://jsfiddle.net/82AsF/(点击导航条上的主题下拉)
给你的链接一类theme-link和像这样一个data-theme值的主题名称:

<a href="#" data-theme="amelia" class="theme-link">Amelia</a> 

然后,定义在你的主题像这样的全局变量:

var themes = { 
    "default": "//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css", 
    "amelia" : "//bootswatch.com/amelia/bootstrap.min.css", 
    "cerulean" : "//bootswatch.com/cerulean/bootstrap.min.css", 
    "cosmo" : "//bootswatch.com/cosmo/bootstrap.min.css", 
    "cyborg" : "//bootswatch.com/cyborg/bootstrap.min.css", 
    "flatly" : "//bootswatch.com/flatly/bootstrap.min.css", 
    "journal" : "//bootswatch.com/journal/bootstrap.min.css", 
    "readable" : "//bootswatch.com/readable/bootstrap.min.css", 
    "simplex" : "//bootswatch.com/simplex/bootstrap.min.css", 
    "slate" : "//bootswatch.com/slate/bootstrap.min.css", 
    "spacelab" : "//bootswatch.com/spacelab/bootstrap.min.css", 
    "united" : "//bootswatch.com/united/bootstrap.min.css" 
} 

(请托管自己的服务器上,这些bootswatch CSS文件,当你做到这一点 - 我只是盗链因为我没有手头上有一台服务器)
然后,使用下面的jQuery代码:

$(function(){ 
    var themesheet = $('<link href="'+themes['default']+'" rel="stylesheet" />'); 
    themesheet.appendTo('head'); 
    $('.theme-link').click(function(){ 
     var themeurl = themes[$(this).attr('data-theme')]; 
     themesheet.attr('href',themeurl); 
    }); 
}); 
+1

感谢答。这真的很有帮助! –

+0

感谢这给了我一个伟大的开始,类似的东西。 –

+0

感谢您的回答Kevin – Harsha

2

与如果页面被刷新主题消失了上述解决方案的主要问题。 我发现这篇文章对于浏览器中的持久主题非常有帮助,因为它将设置保存在浏览器的存储中。

我希望它可以帮助你

http://wdtz.org/bootswatch-theme-selector.html

0

由凯文提供的是伟大的解决方案。我刚刚花了一些时间试图弄清楚如何使它能够与本地的bootswatch库一起工作。

典型的href属性值:

"~/lib/bootstrap/dist/css/bootstrap.min.css" 

但在这种情况下:

 var themes = { 

     "default": "lib/bootstrap/dist/css/bootstrap.min.css", 
     "amelia": "lib/bootswatch/amelia/bootstrap.min.css", 
     "cerulean": "lib/bootswatch/cerulean/bootstrap.min.css", 
     "cosmo": "lib/bootswatch/cosmo/bootstrap.min.css", 
     "cyborg": "lib/bootswatch/cyborg/bootstrap.min.css", 
     "flatly": "lib/bootswatch/flatly/bootstrap.min.css", 
     "journal": "lib/bootswatch/journal/bootstrap.min.css", 
     "readable": "lib/bootswatch/readable/bootstrap.min.css", 
     "simplex": "lib/bootswatch/simplex/bootstrap.min.css", 
     "slate": "lib/bootswatch/slate/bootstrap.min.css", 
     "spacelab": "lib/bootswatch/spacelab/bootstrap.min.css", 
     "united": "lib/bootswatch/united/bootstrap.min.css" 
    } 
0

,你可以尝试这样的事情

HTML:

<link href="/Themes/DefaultClean/Content/css/styles.css" id="theme-sheet" rel="stylesheet" type="text/css" /> 

的javascript:

function setTheme(themeName) { 
    $('#theme-sheet').attr({ href: "https://bootswatch.com/slate/bootstrap.min.css" });; 
} 

其中在setTheme功能将被调用从期望的源,诸如下拉或按钮。

0

我基于@ KevinPei的答案使用jQuery,Bootstrap和Bootswatch自动下拉菜单,将下拉菜单添加到自动更新主题到选定值的导航栏中。

小提琴:https://jsfiddle.net/davfive/uwseLLzf/show

的代码在实践中的伟大工程。然而,在的jsfiddle,有两个奇怪的行为:

  1. 有时候下拉延伸到整个屏幕
  2. 下拉的鼠标悬停/悬停颜色不显示的jsfiddle。

<html lang="en"> 
<head> 
    <!-- To switch themes, go to https://www.bootstrapcdn.com/bootswatch/?theme=0 --> 
    <link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/cerulean/bootstrap.min.css" rel="stylesheet"> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 
<body> 
<div class="navbar navbar-default navbar-static-top"> 
    <div class="container-fluid"> 
    <a class="navbar-brand">Bootswatch Theme Changer</a> 
    <div class="nav navbar-nav pull-right"> 
     <li id="theme-button" class="dropdown"> 
      <a href="#" class="dropdown-toggle" data-toggle="dropdown">Change Theme <b class="caret"></b></a> 
      <ul class="dropdown-menu "> 
      <li><a href="#" name="current">Cerulean</a></li> 
      <li class="divider"></li> 
      <li><a href="#" name="cerulean">Cerulean</a></li> 
      <li><a href="#" name="cosmo">Cosmo</a></li> 
      <li><a href="#" name="cyborg">Cyborg</a></li> 
      <li><a href="#" name="darkly">Darkly</a></li> 
      <li><a href="#" name="flatly">Flatly</a></li> 
      <li><a href="#" name="journal">Journal</a></li> 
      <li><a href="#" name="lumen">Lumen</a></li> 
      <li><a href="#" name="paper">Paper</a></li> 
      <li><a href="#" name="readable">Readable</a></li> 
      <li><a href="#" name="sandstone">Sandstone</a></li> 
      <li><a href="#" name="simplex">Simplex</a></li> 
      <li><a href="#" name="slate">Slate</a></li> 
      <li><a href="#" name="solar">Solar</a></li> 
      <li><a href="#" name="spacelab">Spacelab</a></li> 
      <li><a href="#" name="superhero">Superhero</a></li> 
      <li><a href="#" name="united">United</a></li> 
      <li><a href="#" name="yeti">Yeti</a></li> 
      </ul> 
     </li> 
    </div> 
    </div> 
</div> 
<script> 
jQuery(function($) { 
    $('#theme-button ul a').click(function() { 
    if ($(this).attr('name') != 'current') { 
     var urlbeg = 'https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/' 
     var urlend = '/bootstrap.min.css' 
     var themeurl = urlbeg + $(this).text().toLowerCase() + urlend; 
     var link = $('link[rel="stylesheet"][href$="/bootstrap.min.css"]').attr('href', themeurl); 

     $('#theme-button ul a[name="current"]').text($(this).text()); 
    } 
    }); 
}); 
</script> 
</body> 
</html> 
+0

我无法在我的应用程序上运行您的代码 –

+0

感谢您的支持,并且非常抱歉您不得不这样做。我忘了在引导程序和jquery中包含中的脚本链接。我已经更新并测试了它在jsfiddle之外的作用。 – davfive