【问题标题】:Change <link href=""> by onclick .js通过 onclick .js 更改 <link href="">
【发布时间】:2021-03-08 05:27:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript html hyperlink onclick href


    【解决方案1】:

    使用 if-else:

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

    【讨论】:

      猜你喜欢
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 2018-08-10
      • 2014-09-04
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多