toggle show/hide div with button?

后端 未结 8 1981

Hopefully this is an easy question. I have a div that I want to toggle hidden/shown with a button

8条回答
  •  情深已故
    2020-11-22 14:39

    Pure JavaScript:

    var button = document.getElementById('button'); // Assumes element with id='button'
    
    button.onclick = function() {
        var div = document.getElementById('newpost');
        if (div.style.display !== 'none') {
            div.style.display = 'none';
        }
        else {
            div.style.display = 'block';
        }
    };
    

    SEE DEMO

    jQuery:

    $("#button").click(function() { 
        // assumes element with id='button'
        $("#newpost").toggle();
    });
    

    SEE DEMO

提交回复
热议问题