Show/hide 'div' using JavaScript

前端 未结 14 816
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 04:18

For a website I\'m doing, I want to load one div, and hide another, then have two buttons that will toggle views between the div using JavaScript.

This is my

相关标签:
14条回答
  • 2020-11-22 04:52

    Instead your both functions use toggle function with following body

    this[target].parentNode.style.display = 'none'
    this[source].parentNode.style.display = 'block'
    

    function toggle(target, source) {
      this[target].parentNode.style.display = 'none'
      this[source].parentNode.style.display = 'block'
    }
    <button onClick="toggle('target', 'replace_target')">View Portfolio</button>
    <button onClick="toggle('replace_target', 'target')">View Results</button>
    
    <div>
      <span id="target">div1</span>
    </div>
    
    <div style="display:none">
      <span id="replace_target">div2</span>
    </div>

    0 讨论(0)
  • 2020-11-22 04:58

    You can simply manipulate the style of the div in question...

    document.getElementById('target').style.display = 'none';
    
    0 讨论(0)
提交回复
热议问题