1

我正在使用此 JavaScript 代码从A List Apart更改样式表。

代码如下所示:

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);

现在,在该站点上,建议将此代码添加到您想要链接的 HTML 中:将样式更改为默认值 我尝试使用它,它工作正常。

但我想将我的事件处理程序放在一个单独的文档中,所以我改用它:

<head>

<link href="default.css" rel="stylesheet" type="text/css" />
<link href="theme1.css" title="theme1" rel="stylesheet" type="text/css" />
<link href="theme2.css" title="theme2" rel="stylesheet" type="text/css" />
<link href="theme3.css" title="theme3" rel="stylesheet" type="text/css" />

</head>

<body

<ul id="dropdown">
<li> Choose theme
    <ul> 
        <li id="stylesheet1" > <a href="#"> Default </a></li>
        <li id="stylesheet2" > <a href="#"> Theme 1 </a></li>
        <li id="stylesheet3" > <a href="#"> Theme 2 </a></li>
        <li id="stylesheet4" > <a href="#"> Theme 3 </a></li>
    </ul>
 </li> 

</body>

将此添加到我的 javascript.js 文件中:

function initate()
{
var style1 = document.getElementById("stylesheet1");
var style2 = document.getElementById("stylesheet2");
var style3 = document.getElementById("stylesheet3");
var style4 = document.getElementById("stylesheet4");

style1.onclick = function () { 
    setActiveStyleSheet("default.css"); 
    return false;
    };
style2.onclick = function () { 
    setActiveStyleSheet("theme1.css"); 
    return false;
    };
style3.onclick = function () { 
    setActiveStyleSheet("theme2.css"); 
    return false
    };
style4.onclick = function () { 
    setActiveStyleSheet("theme3.css"); 
    return false
    };          
}

window.onload = initate;

但由于某种原因,这不起作用,我不知道为什么。现在的方式,我的default.csstheme1.css被加载的,当我按下列表中的任何按钮时theme1.css被禁用,当我按下任何其他按钮时没有任何其他反应。

4

1 回答 1

1

该函数setActiveStyleSheet采用titleas 参数。

这是工作代码:

HTML

<head>

<link href="default.css" title="default" rel="stylesheet" type="text/css" />
<link href="theme1.css" title="theme1" rel="stylesheet" type="text/css" />
<link href="theme2.css" title="theme2" rel="stylesheet" type="text/css" />
<link href="theme3.css" title="theme3" rel="stylesheet" type="text/css" />

</head>

Javascript

style1.onclick = function () { 
    setActiveStyleSheet("default"); 
    return false;
    };
style2.onclick = function () { 
    setActiveStyleSheet("theme1"); 
    return false;
    };
style3.onclick = function () { 
    setActiveStyleSheet("theme2"); 
    return false
    };
style4.onclick = function () { 
    setActiveStyleSheet("theme3"); 
    return false
    };          
}
于 2013-01-13T11:36:54.927 回答