如何将字体族切换到不同的按钮元素

huangapple go评论63阅读模式
英文:

How to toggle font-family to different button elements

问题

我想要切换按钮元素的CSS样式。问题是JavaScript代码不起作用。

这是我所做的。changeFont()函数不起作用。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Anton&display=swap" rel="stylesheet">
    <style>
        .invisible {
            font-family: 'Anton', sans-serif;
        }
    </style>
</head>
<body>
    <button onclick="changeFont()">Abraham Abah</button>
    <button onclick="changeFont()">Work</button>
    <button onclick="changeFont()">Lab</button>
    <script>
        function changeFont() {
            let toggleFontFamily = document.querySelectorAll("button");
            for (let i = 0; i < toggleFontFamily.length; i++) {
                if (toggleFontFamily[i] == toggleFontFamily[0]) {
                    toggleFontFamily[0].classList.toggle("invisible");
                } else if (toggleFontFamily[i] == toggleFontFamily[1]) {
                    toggleFontFamily[1].classList.toggle("invisible");
                } else {
                    toggleFontFamily[2].classList.toggle("invisible");
                }
            }
        }
    </script>
</body>
</html>
英文:

I want to toggle a CSS style to the button elements. The problem is that the JavaScript code is not working.

This is what I did. The changeFont() function is not working.

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
    &lt;link href=&quot;https://fonts.googleapis.com/css2?family=Anton&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;style&gt;
        .invisible {
            font-family: &#39;Anton&#39; &#39;sans-serif&#39;;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;button onclick=&quot;changeFont()&quot;&gt;Abraham Abah&lt;/button&gt;
    &lt;button onclick=&quot;changeFont()&quot;&gt;Work&lt;/button&gt;
    &lt;button onclick=&quot;changeFont()&quot;&gt;Lab&lt;/button&gt;
    &lt;script&gt;
        function changeFont() {
            let toggleFontFamily = document.querySelectorAll(&quot;button&quot;);
            for (let i = 0; i &lt; toggleFontFamily.length; i++) {
                if (toggleFontFamily[i] == toggleFontFamily[0]) {
                    toggleFontFamily[0].classList.toggle(&quot;invisible&quot;);
                } else if (toggleFontFamily[i] == toggleFontFamily[1]) {
                    toggleFontFamily[1].classList.toggle(&quot;invisible&quot;);
                } else {
                    toggleFontFamily[2].classList.toggle(&quot;invisible&quot;);
                }
            }
        }
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 2

这比你刚才编写的要简单得多。只需查看我的解决方案。而且你还错过了一个逗号来分隔字体。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Anton&display=swap" rel="stylesheet">
    <style>
        .invisible {
            font-family: 'Anton', 'sans-serif';
        }
    </style>
</head>
<body>
    <button>Abraham Abah</button>
    <button>Work</button>
    <button>Lab</button>
    <script>
        let toggleFontFamily = document.querySelectorAll("button");

        for (i = 0; i < toggleFontFamily.length; i++) {
            toggleFontFamily[i].addEventListener('click', function(){
                this.classList.toggle('invisible');
            })
        }
    </script>
</body>
</html>

请注意,我只翻译了HTML和CSS部分,不包括JavaScript代码。

英文:

It is a lot simpler than what you just coded. Just check my solution. And also you missed a , to separate the fonts.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
    &lt;link href=&quot;https://fonts.googleapis.com/css2?family=Anton&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;style&gt;
        .invisible {
            font-family: &#39;Anton&#39;, &#39;sans-serif&#39;;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;button&gt;Abraham Abah&lt;/button&gt;
    &lt;button&gt;Work&lt;/button&gt;
    &lt;button&gt;Lab&lt;/button&gt;
    &lt;script&gt;
        let toggleFontFamily = document.querySelectorAll(&quot;button&quot;);

        for (i = 0; i &lt; toggleFontFamily.length; i++) {
            toggleFontFamily[i].addEventListener(&#39;click&#39;, function(){
                this.classList.toggle(&#39;invisible&#39;);
            })
        }        
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月10日 22:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定