[jQuery]event.stopPropagation()报错

本文探讨了在使用jQuery处理事件时如何正确阻止事件冒泡。指出若未将event参数传递给事件处理函数,则默认使用window.event,这可能导致兼容性问题。推荐在定义事件处理方法时明确使用event参数。

使用jQuery给一个事件加处理方法时,为了阻止一个事件向上冒泡,使用了event.stopPropagation(),但在IE下却报对象不支持此属性或方法的错误(IE下是event. cancelBubble=true),jQuery不是兼容各浏览器吗?

后来看了下jQuery的官方文档后,原来在使用event的时候,必须在事件处理方法中加入参数event,否则这个event为 window.event,而不是jQuery中的event。所以在jQuery定义事件处理方法时,最好加上参数event,如下:

$('#btn').click(function(event){})  //推荐
$('#btn').click(function(){})  //不推荐

text/x-generic addaccount.js ( ASCII text, with CRLF line terminators ) // Keep your original radio toggle function function toggleRadio(event, element) { //console.log('Radio toggle clicked', element); event.stopPropagation(); // Prevents <summary> toggle const details = element.closest('details'); //console.log('Found parent details for radio:', details); details.classList.toggle('digages-onboard-addacnt-checked'); // Toggle only the clicked item //console.log('Toggled checked class on details'); } // Optional: Explicit chevron toggle document.querySelectorAll('.digages-onboard-addacnt-chevron').forEach(chevron => { chevron.addEventListener('click', (event) => { //console.log('Chevron clicked', chevron); const details = chevron.closest('details'); //console.log('Toggling details open state from', details.open, 'to', !details.open); details.open = !details.open; event.stopPropagation(); // Added to prevent event bubbling }); }); // Function to ensure only one details is open and has the checked class document.addEventListener('DOMContentLoaded', function() { //console.log('DOM fully loaded'); const allDetails = document.querySelectorAll('details'); //console.log('Found', allDetails.length, 'details elements:', allDetails); // Add event listeners for handling accordion behavior allDetails.forEach((details, index) => { //console.log('Setting up listeners for details #', index); // Track the toggle event which fires when open state changes details.addEventListener('toggle', function(event) { //console.log('Toggle event fired on details #', index); //console.log('Current open state:', details.open); if (details.open) { //console.log('This details was opened, closing others'); // Close all other details and remove checked class allDetails.forEach((otherDetails, otherIndex) => { if (otherDetails !== details) { if (otherDetails.open) { //console.log('Closing details #', otherIndex); otherDetails.open = false; } // Remove checked class from all other details if (otherDetails.classList.contains('digages-onboard-addacnt-checked')) { //console.log('Removing checked class from details #', otherIndex); otherDetails.classList.remove('digages-onboard-addacnt-checked'); } } }); // Add checked class to the current details if it doesn't have it if (!details.classList.contains('digages-onboard-addacnt-checked')) { //console.log('Adding checked class to details #', index); details.classList.add('digages-onboard-addacnt-checked'); } } }); }); }); // Add click listener to document to ensure proper class management on summary clicks document.addEventListener('click', function(event) { const clickedSummary = event.target.closest('summary'); if (clickedSummary) { //console.log('Summary element clicked'); const clickedDetails = clickedSummary.closest('details'); // Wait a tiny bit for the default toggle behavior to complete setTimeout(function() { const allDetails = document.querySelectorAll('details'); allDetails.forEach((details) => { // If details is open but not the clicked one, close it and remove class if (details !== clickedDetails && details.open) { details.open = false; details.classList.remove('digages-onboard-addacnt-checked'); } // If it's the clicked details and it's open, ensure it has the class if (details === clickedDetails && details.open) { details.classList.add('digages-onboard-addacnt-checked'); } }); }, 10); } });
最新发布
07-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值