javascript样式表更换器无法正常工作

时间:2012-04-29 22:25:10

标签: javascript css stylesheet alternate

好的,朋友给了我这个代码,但它不起作用。我一直在寻找一个java样式表更换器,这是我能找到的唯一一个。当我点击链接(改变风格)时没有任何反应。救命?而且我在理解javascript时遇到了麻烦,所以对我来说很难。

js code:

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

    }
);

标题样式表代码:

<link rel="stylesheet" type="text/css" media="screen" href="css/zerohour.css" title="default" />

<link rel="alternate stylesheet" type="text/css" media="screen" href="css/rainbow.css" title="rainbow" />

Link Activator代码:

<a href="#" 

onclick="setActiveStyleSheet('default'); 

return false;">change style to default</a>



<a href="#" 

onclick="setActiveStyleSheet('rainbow'); 

return false;">change style to rainbow</a>

2 个答案:

答案 0 :(得分:1)

这是对我认为你要做的事情的重写:

var activeStylesheet = (function() {

  // Store title of default linked stylesheet
  var defaultTitle;

  return {

    // Set the active stylesheet to the link styesheet element with title
    // Set all others to disabled
    setActive: function(title) {
      var link, links = document.getElementsByTagName('link');

      for (var i=0, iLen=links.length; i<iLen; i++) {
        link = links[i];

        if (link.rel.indexOf("style") != -1 && link.title) { 
          link.disabled = true; 

          if (link.title == title) {
            link.disabled = false; 
          }
        }
      }
    },

    // Return the title of the currently active linked stylesheet,
    // or undefined if none found
    getActive: function() {
      var link, links = document.getElementsByTagName('link');

      for (var i=0, iLen=links.length; i<iLen; i++) {
        link = links[i];

        if (link.rel.indexOf("style") != -1 &&
            link.title &&
           !link.disabled) {
          return link.title;
        }
      }
    },

    // Return the title of the link stylesheet element where 
    // rel property contains 'alt'
    getPreferred: function() {
      var link, links = document.getElementsByTagName('link');

      for (var i=0, iLen=links.length; i<iLen; i++) {
        link = links[i];

        if (link.rel.indexOf("style") != -1 && 
            link.rel.indexOf('alt') == -1 &&
            link.title) { 
          defaultTitle = title;
          return link.title; 
        }
      }
    }
  };
}());

要做一些事情的按钮:

<button onclick="
 alert(activeStylesheet.getActive());
">Show title of active stylesheet</button>
<button onclick="
 activeStylesheet.setActive('rainbow');
">Set active stylesheet to "rainbow"</button>
<button onclick="
 activeStylesheet.setActive('default');
">Set active stylesheet to "default"</button>

请注意,使用标题会区分大小写,所以要小心。

您可以在 defaultTitle 变量中存储默认链接样式表的标题,但我不知道您要对它做什么。您有 setPreferred 首先检查 defaultTitle 是否已设置,如果没有,则调用 getPreferred ,使用<调用 setActive EM> defaultTitle

答案 1 :(得分:0)

我意识到这个问题没有被标记Jquery但是除非你有充分的理由坚持使用vanilla Javascript,否则我会推荐Jquery。

您只需两行代码即可切换样式:

$('#foobar').click(function(){
        $('link').attr('href','newstyle.css');
        return false;
});

<强> Live Demo