How TO - 非表示と表示のトグル

❮ 前章へ 次章へ ❯

JavaScript を使用して、要素の非表示と表示をトグルします。


Click the button!

要素の(表示/非表示)のトグル

ステップ 1) HTML の追加:

<button onclick="myFunction()">Click Me</button>

<div id="myDIV">
  This is my DIV element.
</div>

Step 2) Add JavaScript:

function myFunction() {
    var x = document.getElementById('myDIV');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}
Try it Yourself »

❮ 前章へ 次章へ ❯