简体   繁体   English

如何使用 JavaScript 在 Jekyll 中启用禁用的清单?

[英]How do I enable a disabled checklist in Jekyll using JavaScript?

I just need a checkbox that is clickable when rendered.我只需要一个渲染时可点击的复选框。 It seems as though if I create a checkbox using markdown, it does not offer the option to toggle it.好像我使用 markdown 创建了一个复选框,但它不提供切换它的选项。

Jekyll杰基尔

- [ ] Mercury
- [ ] Venus
- [ ] Earth 

I tried using JavaScript but it doesn't seem to be working.我尝试使用 JavaScript 但它似乎不起作用。

JS JS

const checkbox = document.getElementsByClassName(".task-list-item-checkbox");
checkbox.removeAttribute("disabled");

在此处输入图像描述

HTML HTML

<ul class="task-list">
    <li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled="disabled">Mercury</li>
    <li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled="disabled">Venus</li>
    <li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled="disabled">Earth</li>
</ul>

There are two important things what I found:我发现了两件重要的事情:

  1. Once using getElementsByClassName you should pass the class name only without .使用getElementsByClassName后,您应该只传递 class 名称而不传递. , so try instead as .getElementsByClassName('task-list-item-checkbox') . ,所以尝试改为.getElementsByClassName('task-list-item-checkbox')
  2. By definition getElementsByClassName() does not return only one item but instead:根据定义getElementsByClassName()不只返回一项,而是:

The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). Document 接口的getElementsByClassName方法返回一个类似数组的 object,其中包含所有具有给定 class 名称的所有子元素。

Based on my comment you can try as the following:根据我的评论,您可以尝试以下操作:

 const checkboxes = document.getElementsByClassName('task-list-item-checkbox'); Array.prototype.forEach.call(checkboxes, function (e) { e.removeAttribute('disabled'); });
 <ul class="task-list"> <li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled="disabled">Mercury</li> <li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled="disabled">Venus</li> <li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled="disabled">Earth</li> </ul>

I think the above mentioned code snippet gives you the idea where to start once you want to remove disable class from your input elements.我认为上面提到的代码片段让您知道一旦您想从input元素中删除disable class 时从哪里开始。

I hope this helps!我希望这有帮助!

Just in case anyone comes across this in the future, using JQuery works as well.以防万一将来有人遇到这种情况,使用 JQuery 也可以。

$(document).ready(function(){
   $('.task-list-item-checkbox').prop("disabled", false); 
}); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM