英文:
Striking through text when checkbox is checked
问题
以下是您提供的代码部分的中文翻译:
我正在尝试使用express和EJS作为中间件创建一个待办事项列表应用程序。它应该有一个任务列表,每个任务旁边都有一个复选框,已添加的任务可以勾选复选框,文本将被划掉,表示任务已完成。我在我的.ejs文件末尾添加了javascript,但存在一些问题,它无法正常工作。
以下是EJS代码片段:
<% for(var i = listItems2.length -1; i >= 0; i--){ %>
<div class="flex">
<input type="checkbox" name="checker" id="checkbox_id" class="h-7 w-7 m-3 flex-shrink-0">
<p name="para" class="text-[32px] font-[200]"><%= listItems2[i] %></p>
</div>
<% } %>
和JavaScript代码:
<script>
var boxes = document.getElementsByName("checker");
var paras = document.getElementsByName("para");
function updateStyle() {
for (var i = 0; i < boxes.length; i++) {
if (boxes[i].checked) {
paras[i].style.webkitTextStroke = "1px black";
paras[i].style.textStroke = "1px black";
} else {
paras[i].style.webkitTextStroke = "none";
paras[i].style.textStroke = "none";
}
}
}
for (var i = 0; i < boxes.length; i++) {
boxes[i].addEventListener("change", updateStyle);
}
</script>
请注意,我已保留了代码中的HTML和JavaScript标签,并进行了适当的转义,以便在文本中显示。
英文:
I'm trying to make a To-Do list app using express and EJS as middleware. It's supposed to have a list of tasks with a checkbox beside every task that has been added. When the task is done you can check the checkbox and the text will get striked through signifying the completion of the task. I used adding javascript at the end of my .ejs file but there's some problem with it and its not working
here is the EJS code snippet
<% for(var i = listItems2.length -1; i >= 0; i--){ %>
<div class="flex">
<input type="checkbox" name="checker" id="checkbox_id" class="h-7 w-7 m-3 flex-shrink-0">
<p name="para" class="text-[32px] font-[200]"><%= listItems2[i] %></p>
</div>
<% } %>
and the javascript code
<script>
var boxes = document.getElementsByName("checker");
var paras = document.getElementsByName("para");
function updateStyle() {
for (var i = 0; i < boxes.length; i++) {
if (boxes[i].checked) {
paras[i].style.webkitTextStroke = "1px black";
paras[i].style.textStroke = "1px black";
} else {
paras[i].style.webkitTextStroke = "none";
paras[i].style.textStroke = "none";
}
}
}
for (var i = 0; i < boxes.length; i++) {
boxes[i].addEventListener("change", updateStyle);
}
</script>
答案1
得分: 1
给标签添加一个类或样式,使其在复选框被选中时具有删除线样式。
使用 `::before` 和 `::after` 内容元素来提供项目为删除文本的上下文。
英文:
Add a class or style the label to give it strike through styles when the checkbox is checked.
Use ::before
and ::after
content element to provide context that the item is stricken text.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
label {
color: black;
}
#todo {
accent-color: #777
}
#todo:checked+label {
color: #777;
text-decoration: line-through;
&::before,
&::after {
-webkit-clip-path: inset(100%);
clip-path: inset(100%);
clip: rect 1px, 1px, 1px, 1px;
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
&::before {
content: "[start of stricken text]";
}
&::after {
content: "[end of stricken text]";
}
}
<!-- language: lang-html -->
<input type="checkbox" id="todo">
<label for="todo">Todo Item</label>
<!-- end snippet -->
答案2
得分: 0
只需将标签包装在复选框周围(以防止需要for属性和id,但如果您愿意,可以保留它们...然后将标签文本放在一个中,并使用:checked伪选择器和直接相邻的组合符号来通过CSS获得删除线效果。
我已经将其中一个项目设置为已选中,以显示效果,如果浏览器默认样式不符合您的喜好,您可以添加另一个,并在其上使用::before和::after样式来获得样式化的复选框。
<label>
<input type="checkbox" checked>
<span>待办事项</span>
</label>
<label>
<input type="checkbox">
<span>待办事项 2</span>
</label>
<label>
<input type="checkbox">
<span>待办事项 3</span>
</label>
英文:
Just wrap the label around the checkbok (preventing the need for the for attribute and id - but you can leave those in if you want... and then put the label text in a span and use the :checked pseudo selector and direct sibling combinator to get the strike-trhough via css
I have set one item to checked to show the effect and you CAN add another span with the ::before and ::after stylign on it to get a styled checkbox isf the bowser default stylngi is not to your liking.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
label {
color: black;
display: block;
margin-bottom: 8px
}
input:checked+span {
color: #777;
text-decoration: line-through;
}
<!-- language: lang-html -->
<label>
<input type="checkbox" checked>
<span>Todo Item</span>
</label>
<label>
<input type="checkbox">
<span>Todo Item 2</span>
</label>
<label>
<input type="checkbox">
<span>Todo Item 3</span>
</label>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论