2017-02-03 1 views
1

プラス/マイナス記号をクリック可能にするにはどうすればよいですか?それは今ではない... 2番目のボタンをクリックしたときに開いていると、他のボタンをオフ(ドロップダウン)にするにはどうすればよいですか?ここでJavascript - 要素をクリック可能にして他のボタンをオフにする

JAVASCRIPT

$(".dropbtn").append('<span class="adl-signs">&nbsp;+</span>'); 

function ctaDropMenu(e) { 
    e.target.nextElementSibling.classList.toggle("show"); 
} 

function toggleSigns(e) { 
    $(e.target).find('.adl-signs').html(function(_, html) { 
    return $.trim(html) == '&nbsp;+' ? '&nbsp;-' : '&nbsp;+'; 
    }); 
} 

$(".dropbtn").click(function(e) { 
    ctaDropMenu(e) 
    toggleSigns(e) 
}); 

// Close the dropdown if the user clicks outside of it 
window.onclick = function(event) { 
    if (!event.target.matches('.dropbtn')) { 

    var dropdowns = document.getElementsByClassName("dropdown-content"); 
    var i; 
    for (i = 0; i < dropdowns.length; i++) { 
     var openDropdown = dropdowns[i]; 
     if (openDropdown.classList.contains('show')) { 
     openDropdown.classList.remove('show'); 
     } 
    } 
    } 
} 

FIDDLE = https://jsfiddle.net/neuhaus3000/jf1zetLw/8/

答えて

1

が更新fiddleです。私は可能な限りコードを単純化しようとし、どこでも可能な限りjQueryを使用しました。

function changeText(text) { 
    return (text.indexOf('+') >= 0) ? 'Click Me -' : 'Click Me +'; 
} 

$(".dropbtn").click(function() { 

    var $this = $(this), 
    $dropdownActive = $('.dropdown-active'); 

    /* changing the text of the active button (if any) */ 
    $dropdownActive.text(changeText($dropdownActive.text())); 

    /* hiding the content under the active button (if any) */ 
    $('.dropdown-content', $dropdownActive.parent()).toggle('show'); 

    if (!$this.hasClass('dropdown-active')) { 

    /* changing the text of the clicked button */ 
    $this.text(changeText($this.text())); 

    /* showing the content under the clicked button */ 
    $('.dropdown-content', $this.parent()).toggle('show'); 

    /* adding this class to the clicked button */ 
    $this.addClass('dropdown-active'); 
    } 

    /* removing this class from the active button */ 
    $dropdownActive.removeClass('dropdown-active'); 
}); 

// Close the dropdown if the user clicks outside of it 
window.onclick = function(event) { 
    if (!event.target.matches('.dropbtn')) { 

    var $dropdownActive = $('.dropdown-active'); 

    /* changing the text of the active button (if any) */ 
    $dropdownActive.text(changeText($dropdownActive.text())); 

    /* hiding the content under the active button (if any) */ 
    $('.dropdown-content', $dropdownActive.parent()).toggle('show'); 

    /* removing this class from the active button */ 
    $dropdownActive.removeClass('dropdown-active'); 
    } 
} 

問題がある場合は教えてください。ありがとう。

+0

Hey!本当にありがとう!少し問題があります。開いたドロップダウンボタンをクリックして閉じると、閉じられません。それは再開を維持する。感謝万円!!!テキストはボタンごとに異なります... :) –

+0

更新された[fiddle]を確認してください(https://jsfiddle.net/jf1zetLw/11/) –

+0

Super!ありがとうございました!!!テキストに関しては、ボタンごとに違います...;)Merci! :) –

関連する問題