javascript - 如何在两个字体系列之间切换

标签 javascript html css

我正在尝试在字体系列之间进行简单的切换。想法是让用户改变两种字体。目前,我的代码如下所示:

JS

function myFunction() {
    var x = document.getElementById("body")
    if (x.style.fontFamily = "OpenDyslexic") {
        x.style.fontFamily = "OpenDyslexic";
    } else {
        x.style.fontFamily = "Noto";
    }
}

HTML

<label class="switch">
  <input type="checkbox" onclick="myFunction()">
   <span class=" slider round"></span>
</label>

我的代码将字体更改为“OpenDyslexic”,但我无法更改为 Noto 字体,切换不工作。有人可以帮我解决这个问题吗?

最佳答案

你只是错过了 if equals

function myFunction() {
    var x = document.getElementById("body")
    if (x.style.fontFamily == "OpenDyslexic") {
        x.style.fontFamily = "OpenDyslexic";
    } else {
        x.style.fontFamily = "Noto";
    } }

关于javascript - 如何在两个字体系列之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58429062/

相关文章:

javascript - 删除数组中的重复元素

javascript - 从 Google App Engine 中的 .js 文件调用 JavaScript 函数

javascript - 函数链

html - 当我们在css中使用 'overflow'时如何将盒子带到父div之外

html - 如何包装不同背景的内容?

javascript - useState 和 useEffect 之间的区别

ipad - 如何防止 Safari Mobile (iPad) 上的滚动滞后?

javascript - 我的 js 函数没有完美添加

javascript - div 和 p 标签 float 很奇怪而且不正确

html - 所有以开头的标签