动态绑定每个任务div的class对于reminder属性为true的任务
可以使用以下代码:
假设每个任务div的HTML代码如下:
<div class="task" data-reminder="true">Task 1</div>
<div class="task" data-reminder="false">Task 2</div>
<div class="task" data-reminder="true">Task 3</div>
可以使用JavaScript代码动态绑定每个任务div的class:
const tasks = document.querySelectorAll('.task');
tasks.forEach((task) => {
const isReminder = task.getAttribute('data-reminder') === 'true';
if (isReminder) {
task.classList.add('reminder');
}
});
这段代码会遍历每个任务div,判断其data-reminder属性是否为true,如果是,则为该任务div添加一个名为reminder的class。这样,所有reminder属性为true的任务div就会有一个相同的class了。
原文地址: http://www.cveoy.top/t/topic/beFW 著作权归作者所有。请勿转载和采集!