I need to make a block of spoilers that open with a click (there is no problem with this, the code is below). At the same time, by clicking on the icon, the spoiler icon should change from β€œ+” to β€œ-”, and the title of the active spoiler should become bold. after clicking on another spoiler, the previously opened spoiler should close and change its icon back to β€œ+”. in the code below, "+" to "-" changes, but for all spoilers and forever (I'm new to JS and can't figure out how to do it. Help pliz :)

JSfiddle demo: http://jsfiddle.net/a5py9z9v/

jQuery(document).ready(function($) { //SPOILERS var icon = $('.spoiler_links'); icon.find('span').text('+'); $('.spoiler_links').click(function() { $(this).parent().children('div.spoiler_body').toggle('fast'); var icon = $('.spoiler_links'); icon.find('span').text('-'); return false; }); }); 
 .spoiler_body { display:none; } .spoiler_links { cursor:pointer; color:#5f6a70; text-decoration:none; display:block; font-size:1em; } .spoiler_body { font-size:0.9em; } .spoilerblock { padding:1% 0; border-bottom:1px solid #dddede; } .icon { font-size:1.8em; font-weight:bold; color:#51758c; display:inline-block; float:right; position:relative; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <div class="spoilers"> <div class="spoilerblock"> <a href="" class="spoiler_links">Spoiler1 <span class="icon"></span></a> <div class="spoiler_body"> <p> content </p> </div> </div> <div class="spoilerblock"> <a href="" class="spoiler_links">Spoiler2 <span class="icon"></span></a> <div class="spoiler_body"> <p> content </p> </div> </div> <div class="spoilerblock"> <a href="" class="spoiler_links">Spoiler3 <span class="icon"></span></a> <div class="spoiler_body"> <p> content </p> </div> </div> </div> </body> </html> 

    1 answer 1

    Demo 1: http://jsfiddle.net/7qe0tj2u/

     var icon = $('.spoiler_links'); icon.find('span').text('+'); $('.spoiler_links').click(function() { //сначала Π·Π°ΠΊΡ€ΠΎΠ΅ΠΌ всС Π΄Ρ€ΡƒΠ³ΠΈΠ΅ спойлСры var $other_spoilers = $('a.spoiler_links').not(this); $other_spoilers.siblings().hide('fast'); //ΠΈ Π·Π°ΠΎΠ΄Π½ΠΎ Π²Π΅Ρ€Π½Π΅ΠΌ ΠΈΠΌ плюсики $other_spoilers.find('span').text('+'); // Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ собствСнно ΠΎΡ‚ΠΊΡ€ΠΎΠ΅ΠΌ ΠΊΠ»ΠΈΠΊΠ½ΡƒΡ‚Ρ‹ΠΉ спойлСр // кстати, сын ΠΌΠΎΠ΅Π³ΠΎ ΠΎΡ‚Ρ†Π° - ΠΌΠΎΠΉ Π±Ρ€Π°Ρ‚ ;) ΠΏΠΎ-английски - сиблинг // $(this).parent().children('div.spoiler_body').toggle('fast'); $(this).siblings().show('fast') // фикс для минусов // старый ΠΊΠΎΠ΄ Π²Ρ‹Π±ΠΈΡ€Π°Π΅Ρ‚ Π’Π‘Π• спойлСры Π½Π° страницС, Π° Π½Π°ΠΌ Π½ΡƒΠΆΠ΅Π½ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΊΠ»ΠΈΠΊΠ½ΡƒΡ‚Ρ‹ΠΉ, ΠΏΡ€Π°Π²Π΄Π°? //var icon = $('.spoiler_links'); // icon.find('span').text('-'); $(this).find('span').text('-'); return false; }); 
     .spoiler_body { display: none; } .spoiler_links { cursor: pointer; color: #5f6a70; text-decoration: none; display: block; font-size: 1em; } .spoiler_body { font-size: 0.9em; } .spoilerblock { padding: 1% 0; border-bottom: 1px solid #dddede; } .icon { font-size: 1.8em; font-weight: bold; color: #51758c; display: inline-block; float: right; position: relative; } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="spoilers"> <div class="spoilerblock"> <a href="" class="spoiler_links">Spoiler1 <span class="icon"></span></a> <div class="spoiler_body"> <p> content </p> </div> </div> <div class="spoilerblock"> <a href="" class="spoiler_links">Spoiler2 <span class="icon"></span></a> <div class="spoiler_body"> <p> content </p> </div> </div> <div class="spoilerblock"> <a href="" class="spoiler_links">Spoiler3 <span class="icon"></span></a> <div class="spoiler_body"> <p> content </p> </div> </div> </div> 


    The second version, where the open spoiler opens and closes:

     var icon = $('.spoiler_links'); icon.find('span').text('+'); $('.spoiler_links').click(function() { //сначала Π·Π°ΠΊΡ€ΠΎΠ΅ΠΌ всС Π΄Ρ€ΡƒΠ³ΠΈΠ΅ спойлСры var $other_spoilers = $('a.spoiler_links').not(this); $other_spoilers.siblings().hide('fast'); //ΠΈ Π·Π°ΠΎΠ΄Π½ΠΎ Π²Π΅Ρ€Π½Π΅ΠΌ ΠΈΠΌ плюсики $other_spoilers.find('span').text('+'); // Ρ€Π°Π½ΡŒΡˆΠ΅ ΠΌΡ‹ ΠΎΡ‚ΠΊΡ€Ρ‹Π²Π°Π»ΠΈ ΠΊΠ»ΠΈΠΊΠ½ΡƒΡ‚Ρ‹ΠΉ спойлСр //$(this).siblings().show('fast') // Π° Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΌΡ‹ Π΅Π³ΠΎ ΠΏΠ΅Ρ€Π΅ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ - toggle // Π½ΠΎ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π½Π°ΠΌ придСтся-Ρ‚Π°ΠΊΠΈ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ, закрылся ΠΎΠ½ ΠΈΠ»ΠΈ открылся // Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ Π΅ΠΌΡƒ "-" ΠΈΠ»ΠΈ "+" ΠΏΠΎ ΠΎΠΊΠΎΠ½Ρ‡Π°Π½ΠΈΠΈ Π°Π½ΠΈΠΌΠ°Ρ†ΠΈΠΈ $(this).siblings().toggle('fast', function() { var $icon = $(this).siblings('a.spoiler_links').find('span') if ($icon.text() == '+') { $icon.text('-') } else { $icon.text('+') } }) return false; }); 
     .spoiler_body { display: none; } .spoiler_links { cursor: pointer; color: #5f6a70; text-decoration: none; display: block; font-size: 1em; } .spoiler_body { font-size: 0.9em; } .spoilerblock { padding: 1% 0; border-bottom: 1px solid #dddede; } .icon { font-size: 1.8em; font-weight: bold; color: #51758c; display: inline-block; float: right; position: relative; } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="spoilers"> <div class="spoilerblock"> <a href="" class="spoiler_links">Spoiler1 <span class="icon"></span></a> <div class="spoiler_body"> <p> content </p> </div> </div> <div class="spoilerblock"> <a href="" class="spoiler_links">Spoiler2 <span class="icon"></span></a> <div class="spoiler_body"> <p> content </p> </div> </div> <div class="spoilerblock"> <a href="" class="spoiler_links">Spoiler3 <span class="icon"></span></a> <div class="spoiler_body"> <p> content </p> </div> </div> </div> 

    http://jsfiddle.net/wrhqc34c/

    • Thank you very much for the help and PPC (human understandable comments)! Can you tell me how to make it so that you can close the open spoiler? in the original code it was not necessary to additionally register, now, apparently, it is necessary. - Anastasia
    • Added a second version. The first did not remove for comparison and training. - Sergey Snegirev
    • Thank you very much! what you need, and a good plus to my humble knowledge :) - Anastasia