当前位置:网站首页>原生js实现多选框全部选中和取消效果
原生js实现多选框全部选中和取消效果
2022-08-05 01:03:00 【追逐梦想之路_随笔】
html部分
<ul>
<li>全选<input type='checkbox' id='all'></li>
<li>Java<input type='checkbox' class='item'></li>
<li>javaScript<input type='checkbox' class='item'></li>
<li>C++<input type='checkbox' class='item'></li>
<li>python<input type='checkbox' class='item'></li>
<li>.net<input type='checkbox' class='item'></li>
</ul>
javascript部分
<script>
const selectAll = document.querySelector('#all')
const everyItem = Array.from(document.querySelectorAll('.item'))
全部选中和全部取消
selectAll.onchange = () => {
everyItem.forEach(item => {
item.checked = selectAll.checked
})
}
// 单一项选中和取消
everyItem.forEach(item =>{
//每一个元素都绑定监听事件
item.onchange = () => {
// everyItem.every(v => v.checked) 每一项的v.checked都是true 说明返回值是true 否则是false
selectAll.checked = everyItem.every(v => v.checked)
}
})
边栏推荐
- 深度学习训练前快速批量修改数据集中的图片名
- ORA-00257
- 执掌图表
- E - Distance Sequence (prefix and optimized dp
- 【Redis】Linux下Redis安装
- Software testing interview questions: Have you used some tools for software defect (Bug) management in your past software testing work? If so, please describe the process of software defect (Bug) trac
- oracle create tablespace
- gorm joint table query - actual combat
- Use of pytorch: Convolutional Neural Network Module
- The principle of NMS and its code realization
猜你喜欢
随机推荐
第十四天&postman
tensor.nozero(),面具,面具
Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
Software Testing Interview Questions: About Automated Testing Tools?
"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
蓝牙Mesh系统开发五 ble mesh设备增加与移除
ora-00604 ora-02429
Theory of Software Fundamentals
JUC线程池(一): FutureTask使用
Software Testing Interview Questions: Qualifying Criteria for Software Acceptance Testing?
Memory Forensics Series 1
linux(centOs7)部署mysql(8.0.20)数据库
Is DDOS attack really unsolvable?Do not!
从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】
GCC: paths to header and library files
CNI (Container Network Plugin)
2022 The Third J Question Journey
The method of freely controlling concurrency in the sync package in GO
详细全面的postman接口测试实战教程
【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况