通过点击.js更改<链接 href= " " >



我想通过点击图标来更改网站的风格

<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styled.css" id="styles">
</head>

我很难进入href。当时我写了这个剧本:

<script>
function changeTheme(){
document.querySelector("link[href='styled.css']").href = "style.css";
}            
</script>

但它只有一种方式,我想要这样的东西:(第一次单击(style.css->样式表(第二次点击(style.css->style.css。我觉得这个功能启动得不好,所以我请求帮助。有效的脚本应该是什么样子的?

使用if-else:

function changeTheme() {
const link = document.getElementById('styles');
if (link.href.includes('d')) {
link.href = 'style.css';
} else {
link.href = 'styled.css';
}
}

最新更新