当前位置:网站首页>原生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)
}
})
边栏推荐
猜你喜欢
随机推荐
Inter-process communication and inter-thread communication
JUC thread pool (1): FutureTask use
JZ搜索引擎solr研究-从数据库创建索引
跨域解决方案
接口自动化测试框架postman tests常用方法
4. PCIe 接口时序
"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
Activity Recommendation | Kuaishou StreamLake Brand Launch Conference, witness together on August 10!
matlab 采用描点法进行数据模拟和仿真
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
JVM类加载简介
Introduction to JVM class loading
Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?
MongoDB construction and basic operations
2022 The Third J Question Journey
Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
oracle create tablespace
Interview summary: Why do interviewers in large factories always ask about the underlying principles of Framework?
tcp中的三次握手与四次挥手
Lattice PCIe 学习 1








](/img/4d/2d81dc75433c23c5ba6b31453396f0.png)
