I would like a script in JavaScript to change the active style sheet at the click of a button.
(我希望JavaScript中的脚本可以通过单击按钮来更改活动样式表。)
My code is as follows:(我的代码如下:)
index.html file :
(index.html文件:)
<head>
<script type="application/javascript" src="javascript.js">
<link id="pagestyle" href="default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<button id="stylesheet1" onclick="initate();"> Default Style Sheet </button>
<button id="stylesheet2" onclick='onclick="initate();"'> Dark Style Sheet </button>
</body>
javascript.js file :
(javascript.js文件:)
function swapStyleSheet(sheet) {
document.getElementById("pagestyle").setAttribute("href", sheet);
}
function initate() {
var style1 = document.getElementById("stylesheet1");
var style2 = document.getElementById("stylesheet2");
style1.onclick = swapStyleSheet("default.css");
style2.onclick = swapStyleSheet("dark.css");
}
default.css :
(default.css:)
body{
background-color: white;
color: black
}
and, at last, dark.css :
(最后是dark.css:)
body{
background-color: black;
color: white
}
but it doesn't work.
(但这不起作用。)
Can you help me?(你能帮助我吗?)
Thank you.(谢谢。)
ask by MathiAs2Pique_ translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…