如何修改JS实现点击.option按钮时为所有.slide元素添加.hide类
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.slideclass..not(".hide"): Filters that selection to only include elements without the.hideclass..addClass("hide"): Adds the.hideclass 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
相关产品推荐
相关产品推荐

