js other event target

本文介绍了一种解耦导航模式的实现方法,通过定义通用的事件处理函数来响应用户交互,并展示了如何在不同的HTML元素上使用该函数。这种方法有助于提高代码的可维护性和可读性。

 

解耦导航模式

function Onclick(evt)

{

        evt=(evt)?evt:((event)?event:null);

         if(evt)

           {

                 var elem=(evt.target)?evt.target:((evt.srcElement)?evt.srcElement:null);

                 if(elem)

                   {

                          //document.getElementById()

 

                    }

 

            }

 

}

 

<div onclick="Onclick(event)"></div>

 

ID.attachEvent('onclick',Onclick);

(ID)['onclick']=Onclick

 

 

<div onclick="return OnCk(event)"></div>

 

function OnCk(evt)

 

{

          ......

           if(true)

                   return true;

            if(false)

                   return false;

 

}

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、付费专栏及课程。

余额充值