英文:
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.
<!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>
答案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 -->
<!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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论