Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
复选框取消单击事件Javascript_Javascript_Html_Css_Function - Fatal编程技术网

复选框取消单击事件Javascript

复选框取消单击事件Javascript,javascript,html,css,function,Javascript,Html,Css,Function,我有问题,复选框取消选中事件。当我取消选中复选框时,它应该会恢复。我该怎么做 <body> <script> function change() { var cb = document.querySelectorAll('input[type="checkbox"]')[0]; var td = document.querySelectorAll("td[contenteditable]")[0]; cb.add

我有问题,复选框取消选中事件。当我取消选中复选框时,它应该会恢复。我该怎么做

<body>
<script>
 function change()
    {
        var cb = document.querySelectorAll('input[type="checkbox"]')[0];
        var td = document.querySelectorAll("td[contenteditable]")[0];

        cb.addEventListener("click", function () {
            td.className = td.className + " crossed";
        });
    }
</script>
</body>

函数更改()
{
var cb=document.querySelectorAll('input[type=“checkbox”]”)为[0];
var td=document.querySelectorAll(“td[contenteditable]”[0];
cb.addEventListener(“单击”,函数(){
td.className=td.className+“交叉”;
});
}

切换类,如下所示:

cb.addEventListener("click", function () {
   td.classList.toggle("crossed");
});

或者检查是否选中了
复选框

cb.addEventListener("click", function () {
  if(cb.checked) td.classList.add("crossed");
  else td.classList.remove("crossed");
});

如果您想保留,您可以这样做:

   cb.addEventListener("click", function() {
     if (cb.checked) td.className += " crossed";
     else {
       var tdclass = td.className.split(" "),
         ind = tdclass.indexOf("crossed");
       tdclass.splice(ind, 1).join(" ");
       td.className = tdclass;
     }
   });

好的,当你点击一个勾号来取消勾号时,你想重新启用它

$("input[type='checkbox']").props('checked','false') {
    $("input[type='checkbox']").props('checked','true')
}

尝试使用诸如
id
之类的选择器来代替:
input[type='checkbox']

虽然您已经接受了答案,但我建议对以下内容进行微调:

function change() {
    // querySelector() returns the first element matching the
    // selector (or null, if no matching element is found):
    var cb = document.querySelector('input[type="checkbox"]'),
        td = document.querySelector("td[contenteditable]");

    // use the change event on the check-box:
    cb.addEventListener("change", function () {

        // adds, or removes, the class 'crossed'
        // based on the assessment that follows;
        // of the cb node is checked (true) the
        // class is added (if not already present),
        // otherwise it's removed:
        td.classList.toggle('crossed', cb.checked);
    });
}

请编辑您的问题并添加复选框的html。虽然这可能有效,但没有迹象表明OP正在使用jQuery。不应假定可以添加此库。至少应该在您的回答中指出,此方法需要添加额外的JavaScript库,以便代码正确执行……好的,先生,我下次会处理好的