0

好的,朋友给了我这段代码,但它不起作用。我一直在寻找一个 java 样式表转换器,这是我唯一能找到的。当我单击链接(更改样式)时,什么也没有发生。帮助?而且我确实很难理解javascript,所以对我来说很难。

js代码:

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" />

链接激活码:

<a href="#" 

onclick="setActiveStyleSheet('default'); 

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



<a href="#" 

onclick="setActiveStyleSheet('rainbow'); 

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

2 回答 2

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 ,使用defaultTitle调用setActive

于 2012-04-30T03:39:45.633 回答
0

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

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

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

现场演示

于 2012-04-29T22:33:41.077 回答