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

如何修改JS实现点击.option按钮时为所有.slide元素添加.hide类

Solution: Add .hide Class to All .slide Elements on .option Button Click

Hey there! To implement your requirement—adding the .hide class to all .slide divs (only if they don’t already have it) when clicking a .option button—you just need to extend the existing click handler for .option elements. We’ll keep your original functionality (toggling the .window element) intact while adding the new logic.

Modified JavaScript Code

$(".option").on("click", function() {
  // Keep your original window toggle functionality
  $(".window").slideToggle("fast");
  
  // New logic: Add .hide class to all .slide elements that don't have it yet
  $(".slide").not(".hide").addClass("hide");
});

// Keep your existing radio button change handler unchanged
$(':radio').change(function(event) {
  var id = $(this).data('id');
  $('.' + id).removeClass('hide').siblings().addClass('hide');
});

What the New Line Does

  • $(".slide"): Selects all elements with the .slide class.
  • .not(".hide"): Filters that selection to only include elements without the .hide class.
  • .addClass("hide"): Adds the .hide class to those filtered elements, which will hide them thanks to your existing CSS rule .hide { display: none; }.

This way, every time a .option button is clicked, any visible .slide elements will be hidden, while your original window toggle and radio button functionality remains fully functional.

内容的提问来源于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 03:39:58