利用jQuery实现CheckBox全选/全不选/反选

html 代码

<input type="checkbox"  class='item'>苹果</br>
<input type="checkbox"  class='item'>梨子</br>
<input type="checkbox"  class='item'>香蕉</br>
<input type="checkbox"  class='item'>菠萝</br>
<input type="checkbox"  class='item'>芒果</br>
<input type="checkbox"  id='all'>全选/全不选</br>

js 代码( 注意引入 jquery.js )

//全选和全不选
$("#all").click(function()
{
     //item 和 all 保持一致
     $(".item").prop("checked",$(this).prop("checked"));
});

//点击 item 元素时,全选框的变化
$(document).on("click",".item",function()
{
     // 判断 checkbox 被选个数 是否 等于 checkbox 个数
     var flag = $(".item:checked").length==$(".item").length;
     $("#all").prop("checked",flag);
});

 

转载于:https://www.cnblogs.com/fangwu/p/8206055.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 jQuery 实现九九乘法表、全选/全不选反选功能和轮播图的示例代码: 1. 九九乘法表: ```html <!DOCTYPE html> <html> <head> <title>九九乘法表</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <table id="multiplication-table"> <tbody></tbody> </table> <script> $(document).ready(function() { var table = $('#multiplication-table tbody'); for (var i = 1; i <= 9; i++) { var row = $('<tr></tr>'); for (var j = 1; j <= 9; j++) { var cell = $('<td></td>').text(i + ' × ' + j + ' = ' + (i * j)); row.append(cell); } table.append(row); } }); </script> </body> </html> ``` 2. 全选/全不选反选功能: ```html <!DOCTYPE html> <html> <head> <title>全选/全不选反选功能</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <label><input type="checkbox" id="check-all"> 全选/全不选</label><br> <input type="checkbox" class="checkbox-item"> Item 1<br> <input type="checkbox" class="checkbox-item"> Item 2<br> <input type="checkbox" class="checkbox-item"> Item 3<br> <button id="invert-selection">反选</button> <script> $(document).ready(function() { $('#check-all').change(function() { $('.checkbox-item').prop('checked', $(this).prop('checked')); }); $('#invert-selection').click(function() { $('.checkbox-item').each(function() { $(this).prop('checked', !$(this).prop('checked')); }); }); }); </script> </body> </html> ``` 3. 轮播图: ```html <!DOCTYPE html> <html> <head> <title>轮播图</title> <style> .slider { width: 300px; height: 200px; overflow: hidden; position: relative; } .slider img { width: 100%; height: 100%; position: absolute; transition: opacity 1s ease-in-out; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <div class="slider"> <img src="image1.jpg" alt="Image 1"> <img src="image2.jpg" alt="Image 2"> <img src="image3.jpg" alt="Image 3"> </div> <script> $(document).ready(function() { var slider = $('.slider'); var images = slider.find('img'); var currentIndex = 0; function showImage(index) { images.css('opacity', 0); $(images[index]).css('opacity', 1); } function nextImage() { currentIndex = (currentIndex + 1) % images.length; showImage(currentIndex); } setInterval(nextImage, 2000); }); </script> </body> </html> ``` 请注意替换示例中的图片路径和文件名,确保图片可以正确加载。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值