EJS:将样式添加到Infinite中,以通过其ID列表中的项目进行项目

发布于 2025-02-10 05:04:06 字数 1186 浏览 1 评论 0原文

我正在尝试使用其ID字段在网页上的待办事项中添加样式。

<body>
<h1><%- dayType %></h1>
<ul class="tasks">
    <% for(i = 0 ; i < data.length ; i++){ %>
        <input id='label-<%-i+1%>' type='checkbox'/>
        <label for="label-<%-i+1%>"><li><%- data[i] %></li></label>
    <% } %>
    <li>
        <form action="/" method="post">
            <input type="text" placeholder="Enter To Do Item" name="items">
            <input type="submit" value="Submit">
        </form>
    </li>
</ul>
In the code where I am using for loops, there is a checkbox and label to which I am adding styles. My css looks like this:
#label-1:checked ~ label[for=label-1],
#label-2:checked ~ label[for=label-2],
#label-3:checked ~ label[for=label-3],
#label-4:checked ~ label[for=label-4],
#label-5:checked ~ label[for=label-5]{
  background: #6d335c;
  border-bottom: 1px solid #34495E;
  color: #d37b79;
}

当选中“ do do”项目的复选框时,将更改与DO ITEM的样式相对应的。但是,我只能更改有限数量的项目的样式,因为在CSS中,我们必须将每个样式的样式用于执行项目ID。 我希望该网站输入尽可能多的项目,并且其复选框可以完美运行。 如何实现这一目标?

I am trying to add styles to To-do items on my web page using their id field.

<body>
<h1><%- dayType %></h1>
<ul class="tasks">
    <% for(i = 0 ; i < data.length ; i++){ %>
        <input id='label-<%-i+1%>' type='checkbox'/>
        <label for="label-<%-i+1%>"><li><%- data[i] %></li></label>
    <% } %>
    <li>
        <form action="/" method="post">
            <input type="text" placeholder="Enter To Do Item" name="items">
            <input type="submit" value="Submit">
        </form>
    </li>
</ul>

In the code where I am using for loops, there is a checkbox and label to which I am adding styles. My css looks like this:

#label-1:checked ~ label[for=label-1],
#label-2:checked ~ label[for=label-2],
#label-3:checked ~ label[for=label-3],
#label-4:checked ~ label[for=label-4],
#label-5:checked ~ label[for=label-5]{
  background: #6d335c;
  border-bottom: 1px solid #34495E;
  color: #d37b79;
}

When a checkbox of a to do item is checked, the corresponding to do item's style is changed. However, I am able to only change the style of a limited number of to do items because in css, we have to hard code the style for each To do item's id.
I want the website to enter as many to do items as possible and their checkboxes to run perfectly.
How to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人海汹涌 2025-02-17 05:04:06

开始的所有属性中选择所有元素

[attr^=value]

您可以使用一个选择器,该选择器将从指定字符串 noreferrer“> mdn

在您的情况下:

*[id^="label-"]:checked ~ label[for^="label-"]{
  background: #6d335c;
  border-bottom: 1px solid #34495E;
  color: #d37b79;
}

但是,除非您必须通过ID区分这些元素的原因,否则我建议您改用类。然后,您可以绝对确定自己获得了正确的元素集。

You could use a selector which will select all elements with an attribute starting with a specified string

[attr^=value]

see MDN

In your case:

*[id^="label-"]:checked ~ label[for^="label-"]{
  background: #6d335c;
  border-bottom: 1px solid #34495E;
  color: #d37b79;
}

However, unless there is a reason why you have to distinguish between those elements by id, I'd suggest you use a class instead. Then you can be absolutely sure you are getting the right set of elements.

我ぃ本無心為│何有愛 2025-02-17 05:04:06

您不必为每个标签硬编码CSS。您只需将属性添加到&lt; ul class =“ tasks”&gt; parent element的所有子女元素中。您的任务类中的输入标签。

you dont have to hard code CSS for every label. You can just add the properties to all children elements of your <ul class="tasks"> parent element.For e.g .tasks input {} will select all the input tags inside your tasks class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文