div 之间链接跳转操作错误是怎么回事?-灵析社区

博学的学渣

想问一下我做了2个div之间的跳转操作,div之间跳转行为用a标签 href="#"进行跳转,但是我的本意是想做链接来进行跳转操作,不知道为什么点击div的空白处也会进行div的跳转行为 .container { display: flex; justify-content: space-between; } .box { width: 500px; height: 500px; text-align: center; line-height: 100px; color: white; font-size: 18px; cursor: pointer; display: block; } #box1 { background-color: red; } #box2 { background-color: blue; display: none; } #box3 { background-color: green; display: none; } Click to go to Box 2 Click to go to Box 3 You are in Box 3 function handleContainerClick(event) { // 获取点击的元素 const target = event.target; // 判断点击的是否为链接(a 元素) if (target.tagName.toLowerCase() === 'a') { // 链接的默认行为会继续执行 return; } // 如果点击的是包含链接的盒子,则执行链接跳转 if (target.classList.contains('box')) { const boxId = target.id; showBox(boxId); } } function showBox(boxId) { // 隐藏所有的盒子 document.getElementById('box1').style.display = 'none'; document.getElementById('box2').style.display = 'none'; document.getElementById('box3').style.display = 'none'; // 显示指定的盒子 document.getElementById(boxId).style.display = 'block'; } 我做了if判断也没用

阅读量:139

点赞量:0

问AI
没尝试你的 demo,看代码似乎应该是要做选项卡类型的一些效果。 不知道是不是这样的呢。 "http://lab.tianyizone.com/animation%20vs%20transition/animati..." (https://link.segmentfault.com/?enc=t%2FDJ0aIwUjW9Ez2tCSbggA%3D%3D.givBUZO5wROAFbuETd5YcfOIA1JKdnDwhgIDm12eVmhQX1c0uf1mCbrCItaIAs0HNuEaQ3SBtheJtRUyp2EBefPm8ljDSUzI40Nubja2%2Bqk%3D) "http://lab.tianyizone.com/animation%20vs%20transition/transit..." (https://link.segmentfault.com/?enc=cQ3aJu5syhGamKtV%2BYHPhA%3D%3D.ven5xZd6uXptgUtMUBSGByIowGEcrzVj3sPFI%2FQ0DjlzBBJnNcDMlMPmBXxqj6z7%2FPERebl3N5oa%2FpImyXzkKtKHtzJLeMvsjiSC6zApeC0%3D)