用DIV遮罩解决checkbox勾选无效的问题

文章详细介绍了在使用knockout.js进行前端开发时,遇到DIV点击事件与checkbox操作冲突的问题,并提供了变通解决方案,通过在checkbox上覆盖一层div来避免冲突。同时解释了冲突产生的原因及解决思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在前端开发的过程中,遇到一种情况,需要勾选,为了用户的操作便捷就将click事件放到了DIV上。(其中使用了knockout.js)

代码大概如下:

<div id="one" data-biind="click:clickevent">
    <input type="checkbox"><span>有事请勾我</span>
</div>

 但是这样写出现了一个奇怪的现象,鼠标点击div一切正常。

但鼠标直接勾选checkbox不正常:

checkbox处于未勾选状态,鼠标直接点击checkbox勾选,此时应该实现是:1、执行div的clickevent事件;2、事件执行完毕后,checkbox处于勾选状态。

但最终的结果却是,checkbox仍然处于未勾选状态。

跟踪调试结果是,在执行完clickevent事件时,checkbox还是处于勾选状态,但clickevent执行完后,接着进入jquery的代码执行,走了两三步后,checkbox即被改成未选中状态。

原因至今未查到。(另外一个地方使用的radiobox也有类似的情况)

 

没办法,只能变通一下,通过在checkbox上面覆盖一层div,让鼠标点击的时候点的是div而不是checkbox,通过clickevent改变checkbox状态(clickevent事件中本来就有改变checkbox状态的代码)

实现如下:

<div id="one">
    <div id="two" data-bind="click:clickevent"></div>
    <div id="three">
        <input type="checkbox"/> <span>有事请勾我</span>
    </div>
</div>

 ID为two和three的两个div,设置时关键是需要设置两个属性:position:absolute; z-index:1;

其中上面那层的div的z-index属性要比在下面那层的div大。

 

以上DIV的ID属性只是为了说明,一般程序中使用class属性设置。

转载于:https://www.cnblogs.com/rainbow57/p/3912752.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../js/goods.js"></script> <style> .goods-list { width: 100%; height: 300px; border: 1px solid #000; overflow: auto; } .clear::after { content: ""; display: block; clear: both; visibility: hidden; height: 0; } .goods { width: 150px; height: 200px; float: left; overflow: hidden; margin: 10px; } .goods>img { width: 100%; } .goods * { pointer-events: none; } table { width: 100%; border-collapse: collapse; } th, td { /* border: 1px solid #000; */ text-align: center; } .step-num>button { width: 30px; height: 30px; font-size: 25px; line-height: 25px; border-radius: 1; } .step-num>input { width: 50px; height: 25px; font-size: 20px; position: relative; top: -2px; border: 1px solid #000; border-left: none; border-right: none; outline: none; text-align: center; } </style> </head> <body> <div class="goods-list clear"></div> <table> <thead> <tr> <th style="width: 60px"><input type="checkbox" id="all" />全选</th> <th style="width: 80px">商品图</th> <th style="width: 300px">商品信息</th> <th>类型</th> <th>单价</th> <th style="width: 120px">数量</th> <th>合计</th> <th>操作 <button class="alldelete">删除</button></th> </tr> </thead> <tbody></tbody> </table> <img class="view-img" style=" display: none; position: fixed; left: 0; top: 0; bottom: 0; right: 0; margin: auto; " /> <script> var goodsList, tbody, id, all, viewImg, alldelete; var shoppingList = []; init(); function init() { goodsList = document.querySelector('.goods-list'); viewImg = document.querySelector('.view-img'); tbody = document.querySelector('tbody'); all = document.querySelector('#all'); alldelete = document.querySelector('.alldelete'); goodsList.innerHTML = list.map((item) => createGoods(item)).join(""); goodsList.addEventListener('click', addGoodsHandler); tbody.addEventListener('input', inputHandler); tbody.addEventListener('click', tbodyClickHandler); all.addEventListener('change', allChangeHandler); viewImg.addEventListener('click', (e) => (viewImg.style.display = 'none')); alldelete.addEventListener('click', allDeleteHandler); } function allDeleteHandler(e) { shoppingList = shoppingList.filter((item) => !item.checked); render(); } function allChangeHandler(e) { shoppingList.forEach((item) => { item.checked = e.target.checked; }); render(); } function createGoods(item) { return ` <div class='goods' id=${item.proid}> <img src=${item.img1}> <div> <span>${item.proname}</span> </div> </div> `; } function addGoodsHandler(e) { if (e.target.className !== 'goods') return; const id = e.target.id; const shopItem = shoppingList.find((item) => item.id === id); if (!shopItem) { const item = list.find((item) => item.proid == id); const data = { id: item.proid, checked: false, img: item.img1, indo: item.desc, type: item.category, pruce: item.originprice, num: 1, total: item.originprice, }; shoppingList.push(data); } else { shopItem.num++; shopItem.total = shopItem.price * shopItem.num; } render(); } function render() { all.checked = shoppingList.every((item) => item.checked); tbody.innerHTML = shoppingList.map((item) => { return `<tr data=${item.id} > <td><input type="checkbox" class='select' ${item.checked ? "checked" : "" }/></td> <td><img src='${item.img}' width='80'/></td> <td><div style='height:70px;overflow:hidden'>${item.info }</div></td> <td>${item.type}</td> <td>¥${item.price.toFixed(2)}</td> <td class='step-num'><button class='subtraction' ${item.num === 1 ? "disabled" : "" }>-</button><input class='step' type='text' value='${item.num }'/><button class='add'>+</button></td> <td>¥${item.total.toFixed(2)}</td> <td><button class='view'>查看</button><button class='delete'>删除</button></td> </tr>` }).join(''); } function tbodyClickHandler(e) { if (!['BUTTON', 'INPUT'].includes(e.target.nodeName)) return; const id = e.target.parentElement.parentElement.getAttribute('data'); const item = shoppingList.find((item) => item.id == id); switch (e.target.className) { case 'subtraction': if (item.num === 1) return; item.num--; item.total = item.price * item.num; break; case 'add': item.num++; item.total = item.price * item.num; break; case 'select': item.checked = e.target.checked; break; case 'view': viewImg.src = item.img; viewImg.style.display = 'block'; return; case 'delete': shoppingList = shoppingList.filter((item) => item.id != id); break; default: return; } render(); } function inputHandler(e) { if (e.target.className !== 'step' || id) return; id = setTimeout(() => { e.target.value = parseInt(e.target.value) || '1'; const proid = e.target.parentElement.parentElement.getAttribute('data'); const item = shoppingList.find((item) => item.id == proid); item.num = Number(e.target.value); item.total = item.price * item.num; clearTimeout(id); id = undefined; render(); }, 1000); } </script> </body> </html>
最新发布
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值