WordPress动态弹窗关闭按钮失效问题排查求助
WordPress动态弹窗关闭按钮失效问题修复
问题描述
调试这段WordPress自定义文章类型的弹窗代码很久了,弹窗能正常显示、数据加载也没问题,但关闭按钮完全没反应,点击后本该移除.active类来关闭弹窗,就是不生效。
原代码
<?php $panelhead = $args['panelhead']; $panelintro = $args['panelintro']; // NOT HERE! // $teamid = get_the_ID(); ?> <a id="team"></a> <div class="fullcol pb-team-panel pb-padding"> <div class="midcol clearfix"> <div class="fullcol copy-wrapper clearfix"> <div class="team-intro copycol anim-target bottomfade-fm-dm"> <h3><?php echo $panelhead; ?></h3> <p><?php echo $panelintro; ?></p> </div> </div> <div class="fullcol team-grid"> <?php // HERE IS THE QUERY LOOP, YOU NEED TO SET THE ID AFTER THIS QUERY OTHERWISE YOU'LL ONLY BE RETURNING THE ID OF THE CURRENT PAGE AND NOT THE TEAM MEMBER. $recent = new WP_Query("post_type=team&posts_per_page=-1"); while($recent->have_posts()) : $recent->the_post(); // HERE! $teamid = get_the_ID(); $teamimg = get_field('team_image'); ?> <!-- The visible team item(s) --> <div class="team-item anim-target bottomfade-fm-dm"> <a class="trigger" id="<?php echo "trigger-".$teamid; ?>"> <div class="team-image bg-image rounded-box" style="background-image: url(<?php echo $teamimg['url']; ?>);"></div> <h4><?php the_title(); ?></h4> <p><?php the_field('team_jobtitle'); ?></p> </a> </div> <!-- The popup item(s) --> <!-- popup start --> <div class="team-popup target" id="<?php echo "target-".$teamid; ?>"> <div class="overlay"></div> <div class="popup"> <div class="popupcontrols"> <span class="popupclose">X</span> </div> <div class="popupcontent"> <div class="g3"> <img src="<?php echo $teamimg['url']; ?>" /> <a class="nexbtn" href="<?php the_field('team_linkedin'); ?>" target="_blank" rel="noopener noreferrer">Follow <?php the_title(); ?> on LinkedIn</a> </div> <div class="g7"> <h4><?php the_title(); ?></h4> <p><?php the_field('team_bio'); ?></p> </div> </div> </div> </div> <!-- popup end --> <?php endwhile; wp_reset_postdata(); ?> </div> </div> </div> <script type="text/javascript"> // SET POPUP(TARGET) BEHAVIOUR WHEN CLICKING A MATCHING TRIGGER ID $("[id^='trigger-']").on("click", function() { var id = parseInt(this.id.replace("trigger-", ""), 10); var thetarget = $("#target-" + id); if ($(this).hasClass("active")) { // Do something here if the trigger is already active } else { // Do something here if the trigger is not active // Remove active classes from triggers in general $('.trigger').removeClass("active"); // Remove active classes from targets in general $('.target').removeClass("active"); // Add active class to this trigger $(this).addClass("active"); // Add active class to this target $(thetarget).addClass("active"); } }); // Close Popup Event $('.popupclose').on("click", function() { console.log('Button test!'); $('.trigger').removeClass("active"); $('.target').removeClass("active"); }); </script>
问题原因&修复方案
核心问题:事件绑定时机错误
弹窗是通过WordPress循环动态生成的,原JS代码在DOM完成渲染前就执行了,此时.popupclose元素还未被创建,导致点击事件根本绑定不上。修复方法:使用事件委托
将关闭事件绑定到一个已经存在的静态父元素(比如document或者.team-grid)上,通过事件委托来监听动态生成元素的点击事件。
修复后的JavaScript代码
// SET POPUP(TARGET) BEHAVIOUR WHEN CLICKING A MATCHING TRIGGER ID $("[id^='trigger-']").on("click", function() { var id = parseInt(this.id.replace("trigger-", ""), 10); var thetarget = $("#target-" + id); if ($(this).hasClass("active")) { // 新增:点击已激活的触发器时关闭对应弹窗 $(this).removeClass("active"); thetarget.removeClass("active"); } else { // 移除所有触发器和弹窗的active类 $('.trigger').removeClass("active"); $('.target').removeClass("active"); // 给当前触发器和对应弹窗加active类 $(this).addClass("active"); thetarget.addClass("active"); } }); // 改用事件委托绑定关闭按钮点击事件 $(document).on("click", '.popupclose', function() { console.log('Button test!'); $('.trigger').removeClass("active"); $('.target').removeClass("active"); });
额外优化(可选)
可以添加点击遮罩层关闭弹窗、阻止弹窗内部点击冒泡的逻辑,提升用户体验:
// 点击遮罩层关闭弹窗 $(document).on("click", '.team-popup .overlay', function() { $('.trigger').removeClass("active"); $('.target').removeClass("active"); }); // 阻止弹窗内容区域的点击冒泡,避免点击弹窗内部也触发关闭 $(document).on("click", '.popup', function(e) { e.stopPropagation(); });
内容的提问来源于stack exchange,提问作者Lauren
相关产品推荐
相关产品推荐

