[英]Changing Style Sheet javascript
I got this HTML code:我得到了这个 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" > Default Style Sheet </button>
<button id="stylesheet2" > Dark Style Sheet </button>
</body>
One guess would be the missing .css
property, another would be the fact that onclick
is not a function, but a result from invoking one:一种猜测是缺少
.css
属性,另一种猜测是onclick
不是一个函数,而是调用一个函数的结果:
Make all of .css
a string and assign functions to onclick
:使所有
.css
成为字符串并将函数分配给onclick
:
style1.onclick = function () { swapStyleSheet("default.css") };
style2.onclick = function () { swapStyleSheet("dark.css"); };
Transform "default".css into "default.css".将“default”.css 转换为“default.css”。 Do the same for dark.css.
对dark.css 做同样的事情。
Then onclick takes a function as a value.然后 onclick 将函数作为值。
style1.onclick = function () { swapStyleSheet("default.css") };
style2.onclick = function () { swapStyleSheet("dark.css") };
$('button').click(function() {
let mainSheet = $('#pageStyle').attr('href');
if(mainSheet == "default.css")
$('#pageStyle').attr('href','dark.css');
else
$('#pageStyle').attr('href','default.css');
});
Hello it Won't work until you add onclick="" property in html So the final version will look like this: html你好它不会工作直到你在 html 中添加 onclick="" 属性所以最终版本看起来像这样: 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 file Javascript文件
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.