jQuery - Toggle div class on click

前端 未结 3 1371
心在旅途
心在旅途 2021-01-25 07:31

No, this is not a straight forward toggle question. I am aware of the toggle() functions and how to simply hide/show a div. Imagine a box with a label inside:

         


        
相关标签:
3条回答
  • 2021-01-25 08:17
    $('.section').click(function() {
        $(this).removeClass('hidden');
    });
    
    $('.section-legend').click(function(e) {
        var $parent = $(this).parent();
        if(!$parent.hasClass('hidden')) {
            $parent.addClass('hidden');
            return false;
        }
    });
    
    0 讨论(0)
  • 2021-01-25 08:18

    First add an onclick event on the div that needs clicking:

    <div id="myDiv" class="section hidden">
        <div class="section-legend" onclick="function1();">My Section</div>
    </div>
    

    And add this javascript to the html

    <script language="JavaScript">
        function function1(){
            document.getElementById("myDiv").removeAttribute("class");
    
        }
    </script>
    
    0 讨论(0)
  • 2021-01-25 08:29
    $('.section-legend').live('click',function(){
        $(this).parent().toggleClass('hidden');
    });
    

    This is assuming the section-legend is just as large as it's container in 'hidden state'.

    EDIT: changed some code, solution is this:

    $('.section').live('click',function(){
        $(this).removeClass('hidden');
    });
    $('.section-legend').live('click',function(){
        $(this).parent().toggleClass('hidden'); return false;
    });
    

    return false did the trick! demo: http://jsfiddle.net/RUfN7/2/

    0 讨论(0)
提交回复
热议问题