You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于jQuery实现.menu按钮关闭由[name=toggler]单选框激活的.slides容器

Solution for Your Slide Container Control Requirements

Got it, let's tweak your code to perfectly match your needs—making sure the slide containers only open via the [name=toggler] radio buttons and only close via the .menu-btn.

Here's the adjusted code:

// Handle menu button click: Hide the currently visible slide container
$('.menu-btn').click(function() {
  // Target only the visible slide container and hide it
  $('.slide:visible').hide();
  // Optional: Uncheck the corresponding radio button to keep state consistent
  $('[name=toggler]:checked').prop('checked', false);
});

// Handle radio button click: Show the selected slide container (hide others first)
$(function() {
  $("[name=toggler]").click(function() {
    $('.slide').hide();
    $("#blk-" + $(this).val()).show();
  });
});

Key adjustments explained:

  • Menu button behavior: Removed the toggle method (which could accidentally open a slide) and instead explicitly target the visible .slide element to hide it. This ensures only the menu button can trigger the hide action.
  • State consistency: Added a line to uncheck the selected radio button when hiding the slide. This keeps the radio button state in sync with the slide's visibility—no more checked radio buttons pointing to a hidden slide!
  • Radio button behavior: Kept your core logic but confirmed it enforces that only radio buttons can open slides (since clicking a radio button will always hide all slides first, then show the matching one).

Quick note if your container class is .slides instead of .slide:

If your actual container uses the class .slides (matching your requirement description), just update the selectors in the code:

$('.menu-btn').click(function() {
  $('.slides:visible').hide();
  $('[name=toggler]:checked').prop('checked', false);
});

$(function() {
  $("[name=toggler]").click(function() {
    $('.slides').hide();
    $("#blk-" + $(this).val()).show();
  });
});

内容的提问来源于stack exchange,提问作者Kyle Underhill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:35:20