当前位置:网站首页>原生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)
}
})
边栏推荐
- 深度学习:使用nanodet训练自己制作的数据集并测试模型,通俗易懂,适合小白
- 行业现状?互联网公司为什么宁愿花20k招人,也不愿涨薪留住老员工~
- LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
- [230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
- 10年测试经验,在35岁的生理年龄面前,一文不值
- OPENWIFI实践1:下载并编译SDRPi的HDL源码
- [Machine Learning] 21-day Challenge Study Notes (2)
- gorm joint table query - actual combat
- 面试汇总:为何大厂面试官总问 Framework 的底层原理?
- leetcode: 267. Palindromic permutations II
猜你喜欢

自定义线程池

测试工作这么难找吗?今年32,失业2个月,大龄测试工程师接下来该拿什么养家?

"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology

Method Overriding and Object Class

Memory Forensics Series 1

深度学习原理学习小结 - Self-Attention/Transformer

Why is this problem reported when installing oracle11

创意代码表白

gorm joint table query - actual combat

5. PCIe official example
随机推荐
DHCP的工作过程
GCC:头文件和库文件的路径
Difference between MBps and Mbps
linux(centOs7)部署mysql(8.0.20)数据库
tiup status
FSAWS 的全球基础设施和网络
LiveVideoStackCon 2022 上海站明日开幕!
2022 Hangzhou Electric Power Multi-School Session 3 Question B Boss Rush
Day Fourteen & Postman
3. pcie.v file
二叉树[全解](C语言)
码率vs.分辨率,哪一个更重要?
torch.autograd.grad求二阶导数
"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
快速批量修改VOC格式数据集标签的文件名,即快速批量修改.xml文件名
Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
matlab 采用描点法进行数据模拟和仿真
Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
Software Testing Interview Questions: What is Software Testing?The purpose and principle of software testing?
Software testing interview questions: What stages should a complete set of tests consist of?