I am new, but I try to practice in the course of self-study. Tell me, please, I have two textarea and two buttons that only visually refer to these windows. How can I make active the current window when I click on the button that is above it? thank
3 answers
// Π²ΡΠ±ΠΈΡΠ°Π΅ΠΌ Π²ΡΠ΅ ΠΊΠ½ΠΎΠΏΠΊΠΈ ΡΠΎ ΡΡΠ°Π½ΠΈΡΡ const buttons = document.querySelectorAll('button[data-target]'); for (const button of buttons) { // Π²ΡΠ±ΠΈΡΠ°Π΅ΠΌ ΡΠ΅Π»Π΅ΠΊΡΠΎΡ, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΠΈΠΉ ΡΠ΅ΠΊΡΡΠΎΠ²ΠΎΠΌΡ ΠΏΠΎΠ»Ρ const target = button.dataset.target; // Π΅ΡΠ»ΠΈ ΡΠ°ΠΊΠΎΠΉ ΡΠ΅Π»Π΅ΠΊΡΠΎΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ½... if (target) { // Π½Π°Ρ
ΠΎΠ΄ΠΈΠΌ ΡΠ΅ΠΊΡΡΠΎΠ²ΠΎΠ΅ ΠΏΠΎΠ»Π΅ Π΄Π»Ρ ΡΡΠΎΠΉ ΠΊΠ½ΠΎΠΏΠΊΠΈ const textarea = document.querySelector(target); // Π΅ΡΠ»ΠΈ ΠΏΠΎΠ»Π΅ Π½Π°ΡΠ»ΠΎΡΡ if (textarea) { // Π²Π΅ΡΠ°Π΅ΠΌ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΠΎΠ±ΡΡΠΈΡ "ΠΊΠ»ΠΈΠΊ" button.addEventListener('click', event => { // ΠΏΠΎ ΠΊΠ»ΠΈΠΊΡ ΠΌΠ΅Π½ΡΠ΅ΠΌ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΡΠ²ΠΎΠΉΡΡΠ²Π° "Π²ΡΠΊΠ»ΡΡΠ½ΠΎ" Π½Π° ΠΏΡΠΎΡΠΈΠ²ΠΎΠΏΠΎΠ»ΠΎΠΆΠ½ΠΎΠ΅ textarea.disabled = !textarea.disabled; // Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ Π½Π΅ ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎ ΠΏΠΎΠ½ΡΠ», ΡΡΠΎ ΠΈΠΌΠ΅Π½Π½ΠΎ Π½ΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ Ρ // ΡΡΠΈΠΌ ΠΏΠΎΠ»Π΅ΠΌ, Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π²Ρ ΠΈΠΌΠ΅Π»ΠΈ Π² Π²ΠΈΠ΄Ρ, ΡΡΠΎ Π½ΡΠΆΠ½ΠΎ Π½Π°Π²Π΅ΡΡΠΈ ΡΠΎΠΊΡΡ. textarea.focus(); }); } } } <textarea disabled="disabled" id="ta1"></textarea> <button data-target="#ta1">Toggle</button> <br> <textarea id="ta2"></textarea> <button data-target="#ta2">Toggle</button> |
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <textarea class="area_1">1</textarea> <input type="button" class="button_1" placeholder="1"> <textarea class="area_2">2</textarea> <input type="button" class="button_2" placeholder="2"> <script> $(document).on('click','.button_1',function(){ if($('.area_1').hasClass('opened')){ $('.area_1').removeClass('opened'); } else{ $('.area_1').addClass('opened'); $('.area_1').focus(); } }); $(document).on('click','.button_2',function(){ if($('.area_2').hasClass('opened')){ $('.area_2').removeClass('opened'); } else{ $('.area_2').addClass('opened'); $('.area_2').focus(); } }); </script> |
const textarea = document.querySelectorAll('textarea'); [...document.querySelectorAll('button')].forEach((s, i, arr) => { s.addEventListener('click', function() { for (let i = 0; i < textarea.length; i++) { textarea[i].removeAttribute('disabled') } textarea[i].setAttribute('disabled', 'disabled') }) }) <textarea disabled></textarea> <button>Toggle</button> <textarea></textarea> <button>Toggle</button> iandi- to confuse Katya? - Igor- @Igor, this is me on purpose ... There will be something to think about ... - Air
|
testareaelements. This can be special / pair classes assigned to a button andtextarea,data-attributes, common parent. - Igor