用按钮切换显示/隐藏div?

时间:2010-12-24 19:44:34

标签: javascript jquery css

希望这是一个简单的问题。我有一个div,我想用按钮切换隐藏/显示

<div id="newpost">

8 个答案:

答案 0 :(得分:125)

纯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

答案 1 :(得分:62)

查看jQuery Toggle

<强> HTML:

<div id='content'>Hello World</div>
<input type='button' id='hideshow' value='hide/show'>

<强> jQuery的:

jQuery(document).ready(function(){
    jQuery('#hideshow').live('click', function(event) {        
         jQuery('#content').toggle('show');
    });
});

对于jQuery 1.7和更新版本的使用

jQuery(document).ready(function(){
    jQuery('#hideshow').on('click', function(event) {        
        jQuery('#content').toggle('show');
    });
});

如需参考,请查看demo

答案 2 :(得分:25)

JavaScript - 切换Element.styleMDN

&#13;
&#13;
var toggle  = document.getElementById("toggle");
var content = document.getElementById("content");

toggle.addEventListener("click", function() {
  content.style.display = (content.dataset.toggled ^= 1) ? "block" : "none";
});
&#13;
#content{
  display:none;
}
&#13;
<button id="toggle">TOGGLE</button>
<div id="content">Some content...</div>
&#13;
&#13;
&#13;

关于^ bitwise XOR as I/O toggler
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset

JavaScript - 切换.classList.toggle()

&#13;
&#13;
var toggle  = document.getElementById("toggle");
var content = document.getElementById("content");

toggle.addEventListener("click", function() {
  content.classList.toggle("show");
});
&#13;
#content{
  display:none;
}
#content.show{
  display:block; /* P.S: Use `!important` if missing `#content` (selector specificity). */
}
&#13;
<button id="toggle">TOGGLE</button>
<div id="content">Some content...</div>
&#13;
&#13;
&#13;

jQuery - 切换

.toggle()Docs; .fadeToggle()Docs; .slideToggle()Docs

&#13;
&#13;
$("#toggle").on("click", function(){
  $("#content").toggle();                 // .fadeToggle() // .slideToggle()
});
&#13;
#content{
  display:none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="toggle">TOGGLE</button>
<div id="content">Some content...</div>
&#13;
&#13;
&#13;

jQuery - 切换.toggleClass()Docs

.toggle()切换元素的display "block"/"none"

&#13;
&#13;
$("#toggle").on("click", function(){
  $("#content").toggleClass("show");
});
&#13;
#content{
  display:none;
}
#content.show{
  display:block; /* P.S: Use `!important` if missing `#content` (selector specificity). */
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="toggle">TOGGLE</button>
<div id="content">Some content...</div>
&#13;
&#13;
&#13;

HTML5 - 使用<summary><details>

切换

(IE和Opera Mini不支持)

&#13;
&#13;
<details>
  <summary>TOGGLE</summary>
  <p>Some content...</p>
</details>
&#13;
&#13;
&#13;

HTML - 使用checkbox

切换

&#13;
&#13;
[id^=toggle],
[id^=toggle] + *{
  display:none;
}
[id^=toggle]:checked + *{
  display:block;
}
&#13;
<label for="toggle-1">TOGGLE</label>

<input id="toggle-1" type="checkbox">
<div>Some content...</div>
&#13;
&#13;
&#13;

HTML - 使用radio

切换

&#13;
&#13;
[id^=switch],
[id^=switch] + *{
  display:none;
}
[id^=switch]:checked + *{
  display:block;
}
&#13;
<label for="switch-1">SHOW 1</label>
<label for="switch-2">SHOW 2</label>

<input id="switch-1" type="radio" name="tog">
<div>1 Merol Muspi...</div>

<input id="switch-2" type="radio" name="tog">
<div>2 Lorem Ipsum...</div>
&#13;
&#13;
&#13;

CSS - 使用:target

切换

(只是为了确保你的武器库中有它)

&#13;
&#13;
[id^=switch] + *{
  display:none;
}
[id^=switch]:target + *{
  display:block;
}
&#13;
<a href="#switch1">SHOW 1</a>
<a href="#switch2">SHOW 2</a>

<i id="switch1"></i>
<div>1 Merol Muspi ...</div>

<i id="switch2"></i>
<div>2 Lorem Ipsum...</div>
&#13;
&#13;
&#13;

动画类转换

如果您选择一种JS / jQuery方式来实际切换className,您可以随时为元素添加动画过渡,这是一个基本示例:

&#13;
&#13;
var toggle  = document.getElementById("toggle");
var content = document.getElementById("content");

toggle.addEventListener("click", function(){
  content.classList.toggle("appear");
}, false);
&#13;
#content{
  /* DON'T USE DISPLAY NONE/BLOCK! Instead: */
  background: #cf5;
  padding: 10px;
  position: absolute;
  visibility: hidden;
  opacity: 0;
          transition: 0.6s;
  -webkit-transition: 0.6s;
          transform: translateX(-100%);
  -webkit-transform: translateX(-100%);
}
#content.appear{
  visibility: visible;
  opacity: 1;
          transform: translateX(0);
  -webkit-transform: translateX(0);
}
&#13;
<button id="toggle">TOGGLE</button>
<div id="content">Some Togglable content...</div>
&#13;
&#13;
&#13;

答案 3 :(得分:13)

这是一种简单的Javascript切换方式:

<script>
  var toggle = function() {
  var mydiv = document.getElementById('newpost');
  if (mydiv.style.display === 'block' || mydiv.style.display === '')
    mydiv.style.display = 'none';
  else
    mydiv.style.display = 'block'
  }
</script>

<div id="newpost">asdf</div>
<input type="button" value="btn" onclick="toggle();">

答案 4 :(得分:0)

这就是我使用类隐藏和显示内容的方法。将班级更改为空将更改显示为阻止,将班级更改为&#39; a&#39;将显示为无。

<!DOCTYPE html>
<html>
<head>
<style>
body  {
  background-color:#777777;
  }
block1{
  display:block; background-color:black; color:white; padding:20px; margin:20px;
  }
block1.a{
  display:none; background-color:black; color:white; padding:20px; margin:20px;
  }
</style>
</head>
<body>
<button onclick="document.getElementById('ID').setAttribute('class', '');">Open</button>
<button onclick="document.getElementById('ID').setAttribute('class', 'a');">Close</button>
<block1 id="ID" class="a">
<p>Testing</p>
</block1>
</body>
</html>

答案 5 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#hideshow').click(function(){
    $('#content').toggle('show');
  });
});
</script>

和html

<div id='content'>Hello World</div>
<input type='button' id='hideshow' value='hide/show'>

答案 6 :(得分:0)

尝试不透明

div { transition: all 0.4s ease }
.hide { opacity: 0; }
<input onclick="newpost.classList.toggle('hide')" type="button" value="toggle">

<div id="newpost">Hello</div>

答案 7 :(得分:-1)

您可以使用以下内容:

mydiv.style.display === 'block' = (mydiv.style.display === 'block' ? 'none' : 'block');