javascript - 我想使用按钮更改我的 UL 中的某些名称
问题描述
我想使用单击按钮将 UL 中的 9 个名称更改为红色字体,而其他 3 个名称仍为黑色字体。我想要一个按钮将红色字体重置为原始字体。任何人都可以帮忙吗?
var title = document.getElementById("title");
var buttons = document.getElementsByClassName("btn");
var buttons = document.getElementsByClassName("btn2");
for (var btnIndex = 0; btnIndex < buttons.length; btnIndex++) {
buttons[btnIndex].onclick = function() {
title.style.color = this.getAttribute('data-color');
}
} else {
title1.style.color = this.getAttribute('data-color');
}
<ul>
<li id="title">John</li>
<li id="title">Jack</li>
<li id="title">Joe</li>
<li id="title1">Jim</li>
<li id="title">David</li>
<li id="title">Sam</li>
<li id="title1">Jay</li>
<li id="title">Frank</li>
<li id="title">Tim</li>
<li id="title">Zack</li>
<li id="title">Lewis</li>
<li id="title1">Danny</li>
<button class="btn" data-color="red">Change 9 names to red</button>
<button class="btn2" data-color="black">Reset</button>
</ul>
解决方案
您的标记存在一些问题,我将在下面解决,但要回答您的实际问题,您可以执行以下操作:
// get references to the buttons
const button1 = document.querySelector('.btn');
const button2 = document.querySelector('.btn2');
// declare a function that adds the class 'red' to items matching the given selector
const select = selector => {
[...document.querySelectorAll(selector)].forEach(
element => element.classList.add('red')
);
}
// declare a function that removes the given class from all elements that currently have it
const deselect = className => {
[...document.querySelectorAll('.' + className)].forEach(
element => element.classList.remove(className)
);
}
// add a click handler to the button that invokes the
// select function above for items whose class includes 'title'
button1.addEventListener('click', () => select('.title'));
// add a click handler to the second button that removes the 'red' class from all items
button2.addEventListener('click', () => deselect('red'));
.red {
color: red;
}
<ul>
<li class="title">John</li>
<li class="title">Jack</li>
<li class="title">Joe</li>
<li class="title1">Jim</li>
<li class="title">David</li>
<li class="title">Sam</li>
<li class="title1">Jay</li>
<li class="title">Frank</li>
<li class="title">Tim</li>
<li class="title">Zack</li>
<li class="title">Lewis</li>
<li class="title1">Danny</li>
</ul>
<button class="btn" data-color="red">Change 9 names to red</button>
<button class="btn2" data-color="black">Reset</button>
更有效的解决方案
这可能不适合您的需求,但如果您只想更改title
项目的颜色,您可以在 上切换类<ul>
并应用 css 规则:
// get references to the button and ul
const button = document.querySelector('.btn');
const ul = document.querySelector('ul');
// toggle a class on the ul
button.addEventListener('click', () => ul.classList.toggle('red'));
/*
color 'title' items when the
ul has the 'red' class
*/
ul.red .title {
color: red;
}
<ul>
<li class="title">John</li>
<li class="title">Jack</li>
<li class="title">Joe</li>
<li class="title1">Jim</li>
<li class="title">David</li>
<li class="title">Sam</li>
<li class="title1">Jay</li>
<li class="title">Frank</li>
<li class="title">Tim</li>
<li class="title">Zack</li>
<li class="title">Lewis</li>
<li class="title1">Danny</li>
</ul>
<button class="btn">Toggle 'title' items to red</button>
标记问题
id
属性在文档中必须是唯一的。如果您需要将相同的标识符附加到多个元素,请class
改用。<button>
不能是 的孩子<ul>
。
推荐阅读
- flutter - Flutter 条件小部件但 {} 不等于 {}
- amazon-dynamodb - DynamoDB 表大小
- ios - 实例化视图控制器时出现 TypeMismatch 错误
- java - 制作一种在字符串中查找小写字母的方法
- mysql - 使用 MySql 5.7 中的动态列引用从另一个表更新表
- android - Android:如何使用 ViewModel 清除 Recyclerview 中的 Spannable 颜色?
- amazon-web-services - AWS ECS Fargate:应用程序负载均衡器返回 503 并带有奇怪的模式
- sql - 有没有办法在 PostgreSQL 函数中查询当前行值以及来自同一表的过滤结果集
- r - 如何对文件夹中的所有文件应用相同的转换
- swift - 如何在两个日期之间获取每个星期五的日期?