简体   繁体   English

Javascript点击功能一次只选择第一个元素或所有元素

[英]Javascript click function only selects the first element or all elements at once

I made a simple unordered list with checkboxes and want a class with strikethrough text to be applied to the li that the checkbox belongs to.我制作了一个带有复选框的简单无序列表,并希望将带有删除线文本的类应用于复选框所属的 li。 The methods I've tried either only work on the first element or get applied to all of them at once.我尝试过的方法要么仅适用于第一个元素,要么立即应用于所有元素。

The HTML: HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>
<body>
  <h1>Todo List</h1>
  <ul>
    <li>
      <input type="checkbox">
      List Item 1
    </li>
    <li>
      <input type="checkbox">
      List Item 2
    </li>
    <li>
      <input type="checkbox">
      List Item 3
    </li>
  </ul>
</body>
</html>

The Javascript (only selects the first one): Javascript(只选择第一个):

document.querySelector("input").addEventListener("click", addStrike);

function addStrike() {
  document.querySelector("li").classList.toggle("completed");
}

(selects all at once, when using this code I had the jquery CDN in my html) (一次全部选择,使用此代码时,我的 html 中有 jquery CDN)

$("li").on("click", addStrike);

function addStrike() {
  $("li").toggleClass("completed")
}

You need to select all the checkboxes, loop over them, and attach the event.您需要选择所有复选框,遍历它们并附加事件。

 function cbClicked (event) { // reference the checkbox var cb = event.target // find it was checked var isChecked = cb.checked cb.closest("li") // find the parent li .classList.toggle("done", isChecked) // toggle the class } // Select all the checkboxes var checkboxes = document.querySelectorAll('ul input[type="checkbox"]') // loop over all the checkboxes checkboxes.forEach( function (cb) { // attach the event listener to each one cb.addEventListener("change", cbClicked) })
 .done { text-decoration: line-through; }
 <ul> <li> <input type="checkbox"> List Item 1 </li> <li> <input type="checkbox"> List Item 2 </li> <li> <input type="checkbox"> List Item 3 </li> </ul>

If you want to use jQuery it is similar, jQuery just does the looping for you如果你想使用 jQuery 它是类似的,jQuery 只是为你做循环

 function cbClicked (event) { // reference the checkbox var cb = this var isChecked = this.checked $(cb).closest("li") // find the parent li .toggleClass("done", isChecked) // toggle the class } // Select all the checkboxes var checkboxes = $('ul input[type="checkbox"]').on("change", cbClicked)
 .done { text-decoration: line-through; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li> <input type="checkbox"> List Item 1 </li> <li> <input type="checkbox"> List Item 2 </li> <li> <input type="checkbox"> List Item 3 </li> </ul>

And it can be done with Just CSS它可以用 Just CSS 来完成

 input:checked + label { text-decoration: line-through; }
 <ul> <li> <input type="checkbox" id="cb1"> <label for="cb1">List Item 1</label> </li> <li> <input type="checkbox" id="c2"> <label for="cb2">List Item 2</label> </li> <li> <input type="checkbox" id="cb3"> <label for="cb3">List Item 3</label> </li> </ul>

If you would rather apply the class 'completed' (and for that the style) when the check box is checked, I would recommend doing this, with jQuery:如果您更愿意在选中复选框时应用“已完成”类(以及样式),我建议使用 jQuery 执行此操作:

$(".strikeCheckbox").change(function() {
    if(this.checked) {
        $(this).parent().addClass("completed")
    } else {
        $(this).parent().removeClass("completed")
    }
});

You can change the parent function for a specific prev(...) if you want.如果需要,您可以更改特定prev(...)的父函数。

Add an Id to the UL and then use the below javascript向 UL 添加一个 Id,然后使用下面的 javascript

document.getElementById('ele').addEventListener('click',function(e){
    event.target.parentElement.classList.toggle('completed');
});

document.querySelector always returns the first element that matches the given selector document.querySelector总是返回匹配给定选择器的第一个元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法获取api数组以从数组输出1个第一个元素,它只能一次在jquery / javascript中输出所有元素 - cant get api array to output 1 first element from array, it only outputs all elements at once in jquery/javascript Javascript 单击功能仅适用于第一个元素 - Javascript Click Function Only Works On First Element JavaScript函数仅适用于具有类名称的第一个元素,而不适用于具有该类的所有元素 - JavaScript function is only working for the first element with a class name, not all elements with that class 仅在第一次单击时更改元素边距的 JavaScript 函数 - JavaScript function to change margin of element only working on first click 有没有一种方法可以使单击元素数组时在javascript函数内添加的元素仅添加一次? - Is there a way so that elements which are getting added inside javascript function on the click of array of elements should be added only once? 点击元素上的Div元素仅触发第一个元素 - Div elements on click fires for the first element only Javascript点击功能只能工作一次 - Javascript click function only work once 元素在JavaScript函数中仅更改一次 - Element changed only once in JavaScript function jQuery 点击功能仅适用于第一个元素 - jQuery click function only works on first element jQuery单击功能仅激活第一个元素 - jQuery click function only activating first element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM