英文:
Reverting the style of the class
问题
我正在学习如何编写代码,不幸的是,我在一个练习中遇到了困难。
我有一个包含两行不同颜色的table
,我想要反转颜色 - tr
类.vermelho
需要变成.azul
,.azul
变成.vermelho
。
我尝试了以下JS代码,但它将整个table
都设置为单一颜色。你能否帮助我找出JS代码中的错误?对于任何英语拼写错误,我不是母语,深感抱歉:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function invertStyle() {
if ($('.vermelho').hasClass('vermelho')) {
$('.vermelho').removeClass('vermelho').addClass('azul');
} else if ($('.azul').hasClass('azul')) {
$('.azul').removeClass('azul').addClass('vermelho');
}
}
<!-- language: lang-css -->
table {
border-collapse: collapse;
text-align: center;
width: 100px;
}
.vermelho {
background-color: red;
}
.azul {
background-color: blue;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<table id="tabela">
<tr>
<th>Nome</th>
<th>Idade</th>
</tr>
<tr class="azul">
<td>João</td>
<td>40</td>
</tr>
<tr class="vermelho">
<td>Maria</td>
<td>50</td>
</tr>
<tr class="azul">
<td>Ana</td>
<td>20</td>
</tr>
<tr class="vermelho">
<td>Pedro</td>
<td>10</td>
</tr>
<tr class="azul">
<td>Lúcio</td>
<td>25</td>
</tr>
<tr class="vermelho">
<td>Júlia</td>
<td>15</td>
</tr>
</table>
<input type="button" value="Inverter estilo" onClick="invertStyle()" />
</body>
<!-- end snippet -->
以上是您提供的代码部分的翻译。
英文:
I am learning how to code and unfortunately, I am stuck on one exercise.
I have a table
containing 2 different rows of colors, and I want to invert the colors — the tr
class .vermelho
needs to be .azul
, and .azul
to be .vermelho
.
I tried the following JS code, but it's setting the whole table
with a single color. Would you please be so kind to help me identify the error on the JS code? Sorry for any English typo I am not native:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function invertStyle() {
if ($('.vermelho').hasClass('vermelho')) {
$('.vermelho').removeClass('vermelho').addClass('azul');
} else if ($('.azul').hasClass('azul')) {
$('.azul').removeClass('azul').addClass('vermelho');
}
}
<!-- language: lang-css -->
table {
border-collapse: collapse;
text-align: center;
width: 100px;
}
.vermelho {
background-color: red;
}
.azul {
background-color: blue;
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<table id="tabela">
<tr>
<th>Nome</th>
<th>Idade</th>
</tr>
<tr class="azul">
<td>João</td>
<td>40</td>
</tr>
<tr class="vermelho">
<td>Maria</td>
<td>50</td>
</tr>
<tr class="azul">
<td>Ana</td>
<td>20</td>
</tr>
<tr class="vermelho">
<td>Pedro</td>
<td>10</td>
</tr>
<tr class="azul">
<td>Lúcio</td>
<td>25</td>
</tr>
<tr class="vermelho">
<td>Júlia</td>
<td>15</td>
</tr>
</table>
<input type="button" value="Inverter estilo" onClick="invertStyle()" />
<!-- end snippet -->
答案1
得分: 1
选择所有tr
,除了第一行,然后在每次点击时切换它们的类。
请参考下面的代码片段。简而言之,我已经像下面这样解决了问题:
- 将所有
tr
行存储在变量rows
中。 - 每次按钮被点击时,迭代
tr
列表以检查它们当前的类名。- 如果当前是
azul
,则将其更改为vermelho
。 - 如果当前不是
azul
(即为vermelho
),则将其更改为azul
。
- 如果当前是
我尽量使一切都清晰易懂,唯一可能让人困惑的是三元运算符。请参阅Ternary Conditional的MDN文档。
const button = document.querySelector('input[type="button"]');
const rows = document.querySelectorAll('tr:not(:first-child)');
button.addEventListener('click', function() {
rows.forEach(row => {
const currentClass = row.className;
row.className = currentClass === 'azul' ? 'vermelho' : 'azul';
});
});
table {
border-collapse: collapse;
text-align: center;
width: 100px;
}
.vermelho {
background-color: red;
}
.azul {
background-color: blue;
}
<table id="tabela">
<tr>
<th>Nome</th>
<th>Idade</th>
</tr>
<tr class="azul">
<td>João</td>
<td>40</td>
</tr>
<tr class="vermelho">
<td>Maria</td>
<td>50</td>
</tr>
<tr class="azul">
<td>Ana</td>
<td>20</td>
</tr>
<tr class="vermelho">
<td>Pedro</td>
<td>10</td>
</tr>
<tr class="azul">
<td>Lúcio</td>
<td>25</td>
</tr>
<tr class="vermelho">
<td>Júlia</td>
<td>15</td>
</tr>
</table>
<input type="button" value="Inverter estilo" />
希望这有助于你理解代码。
英文:
Select all tr
except the first row, and toggle the class on each click.
Please see the code snippet below. In short, I've solved like the following:
- Store all
tr
rows in a variablerows
- Each time the button is clicked, iterate the
tr
list to check their current class name.- If currently
azul
, make itvermelho
. - If currently not
azul
(i.e. it'svermelho
), make itazul
.
- If currently
I tried to make everything self-explanatory, and the only potentially confusing thing might be the ternary operator. See MDN Docs for Ternary Conditional.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const button = document.querySelector('input[type="button"]');
const rows = document.querySelectorAll('tr:not(:first-child)');
button.addEventListener('click', function() {
rows.forEach(row => {
const currentClass = row.className;
row.className = currentClass === 'azul' ? 'vermelho' : 'azul';
});
});
<!-- language: lang-css -->
table {
border-collapse: collapse;
text-align: center;
width: 100px;
}
.vermelho {
background-color: red;
}
.azul {
background-color: blue;
}
<!-- language: lang-html -->
<table id="tabela">
<tr>
<th>Nome</th>
<th>Idade</th>
</tr>
<tr class="azul">
<td>João</td>
<td>40</td>
</tr>
<tr class="vermelho">
<td>Maria</td>
<td>50</td>
</tr>
<tr class="azul">
<td>Ana</td>
<td>20</td>
</tr>
<tr class="vermelho">
<td>Pedro</td>
<td>10</td>
</tr>
<tr class="azul">
<td>Lúcio</td>
<td>25</td>
</tr>
<tr class="vermelho">
<td>Júlia</td>
<td>15</td>
</tr>
</table>
<input type="button" value="Inverter estilo" />
<!-- end snippet -->
答案2
得分: 0
我使用了以下的JS代码,并且它有效:
function invertStyle() {
$('.vermelho, .azul').each(function() {
if ($(this).hasClass('vermelho')) {
$(this).removeClass('vermelho').addClass('azul');
} else if ($(this).hasClass('azul')) {
$(this).removeClass('azul').addClass('vermelho');
}
});
}
html, body {
height: 100%;
width: 100%;
}
table{
border-collapse: collapse;
text-align: center;
width: 100px;
}
.vermelho{
background-color: #f96c6c;
}
.azul{
background-color: #5757ff;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Exercise</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<table id="tabela">
<tr>
<th>Nome</th>
<th>Idade</th>
</tr>
<tr class="azul">
<td>João</td>
<td>40</td>
</tr>
<tr class="vermelho">
<td>Maria</td>
<td>50</td>
</tr>
<tr class="azul">
<td>Ana</td>
<td>20</td>
</tr>
<tr class="vermelho">
<td>Pedro</td>
<td>10</td>
</tr>
<tr class="azul">
<td>Lúcio</td>
<td>25</td>
</tr>
<tr class="vermelho">
<td>Júlia</td>
<td>15</td>
</tr>
</table>
<hr>
<input type="button" value="Inverter estilo" onClick="invertStyle()" />
</body>
</html>
英文:
I ended up using the following JS code, and it worked:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function invertStyle() {
$('.vermelho, .azul').each(function() {
if ($(this).hasClass('vermelho')) {
$(this).removeClass('vermelho').addClass('azul');
} else if ($(this).hasClass('azul')) {
$(this).removeClass('azul').addClass('vermelho');
}
});
}
<!-- language: lang-css -->
html, body {
height: 100%;
width: 100%;
}
table{
border-collapse: collapse;
text-align: center;
width: 100px;
}
.vermelho{
background-color: #f96c6c;
}
.azul{
background-color: #5757ff;
}
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Exercise</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<table id="tabela">
<tr>
<th>Nome</th>
<th>Idade</th>
</tr>
<tr class="azul">
<td>João</td>
<td>40</td>
</tr>
<tr class="vermelho">
<td>Maria</td>
<td>50</td>
</tr>
<tr class="azul">
<td>Ana</td>
<td>20</td>
</tr>
<tr class="vermelho">
<td>Pedro</td>
<td>10</td>
</tr>
<tr class="azul">
<td>Lúcio</td>
<td>25</td>
</tr>
<tr class="vermelho">
<td>Júlia</td>
<td>15</td>
</tr>
</table>
<hr>
<input type="button" value="Inverter estilo" onClick="invertStyle()" />
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论