버튼으로 div 표시 / 숨기기 전환?
바라건대 이것은 쉬운 질문입니다. 나는이 div
나는 버튼으로 표시 / 숨김 전환 할 것인지
<div id="newpost">
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');
});
});
참고로이 데모를 확인하십시오.
순수 자바 스크립트 :
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';
}
};
데모보기
jQuery :
$("#button").click(function() {
// assumes element with id='button'
$("#newpost").toggle();
});
데모보기
JavaScript- MDN 토글Element.style
var toggle = document.getElementById("toggle");
var content = document.getElementById("content");
toggle.addEventListener("click", function() {
content.style.display = (content.dataset.toggled ^= 1) ? "block" : "none";
});
#content{
display:none;
}
<button id="toggle">TOGGLE</button>
<div id="content">Some content...</div>
^
I / O 토글 러로서의 비트 XOR 정보
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset
JavaScript-토글 .classList.toggle()
var toggle = document.getElementById("toggle");
var content = document.getElementById("content");
toggle.addEventListener("click", function() {
content.classList.toggle("show");
});
#content{
display:none;
}
#content.show{
display:block; /* P.S: Use `!important` if missing `#content` (selector specificity). */
}
<button id="toggle">TOGGLE</button>
<div id="content">Some content...</div>
jQuery-토글
.toggle()
문서 ; .fadeToggle()
문서 ; .slideToggle()
문서
$("#toggle").on("click", function(){
$("#content").toggle(); // .fadeToggle() // .slideToggle()
});
#content{
display:none;
}
<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>
jQuery- 문서 토글.toggleClass()
.toggle()
요소의 display
"block"/"none"
값을 토글 합니다.
$("#toggle").on("click", function(){
$("#content").toggleClass("show");
});
#content{
display:none;
}
#content.show{
display:block; /* P.S: Use `!important` if missing `#content` (selector specificity). */
}
<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>
HTML5- <summary>
및 사용 전환<details>
(IE 및 Opera Mini에서는 지원되지 않음)
<details>
<summary>TOGGLE</summary>
<p>Some content...</p>
</details>
HTML-사용 전환 checkbox
[id^=toggle],
[id^=toggle] + *{
display:none;
}
[id^=toggle]:checked + *{
display:block;
}
<label for="toggle-1">TOGGLE</label>
<input id="toggle-1" type="checkbox">
<div>Some content...</div>
HTML-다음을 사용하여 전환 radio
[id^=switch],
[id^=switch] + *{
display:none;
}
[id^=switch]:checked + *{
display:block;
}
<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>
CSS-다음을 사용하여 전환 :target
(당신이 당신의 무기고에 그것을 가지고 있는지 확인하기 위해)
[id^=switch] + *{
display:none;
}
[id^=switch]:target + *{
display:block;
}
<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>
클래스 전환 애니메이션
실제로를 토글하는 JS / jQuery 방법 중 하나를 선택 className
하면 항상 요소에 애니메이션 전환을 추가 할 수 있습니다. 다음은 기본 예입니다.
var toggle = document.getElementById("toggle");
var content = document.getElementById("content");
toggle.addEventListener("click", function(){
content.classList.toggle("appear");
}, false);
#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);
}
<button id="toggle">TOGGLE</button>
<div id="content">Some Togglable content...</div>
다음은 토글을 수행하는 일반적인 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();">
이것이 수업을 사용하여 콘텐츠를 숨기고 표시하는 방법입니다. 클래스를 없음으로 변경하면 디스플레이가 차단으로 변경되고 클래스를 'a'로 변경하면 디스플레이가 없음으로 표시됩니다.
<!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>
<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'>
불투명도로 시도
div { transition: all 0.4s ease }
.hide { opacity: 0; }
<input onclick="newpost.classList.toggle('hide')" type="button" value="toggle">
<div id="newpost">Hello</div>
다음을 사용할 수 있습니다.
mydiv.style.display === 'block' = (mydiv.style.display === 'block' ? 'none' : 'block');
참고 URL : https://stackoverflow.com/questions/4528085/toggle-show-hide-div-with-button
'Development Tip' 카테고리의 다른 글
녹색에서 빨간색으로 백분율에 따라 다름 (0) | 2020.11.12 |
---|---|
!! 1 ==“1”이 true이고 !! 2 ==“2”가 false 인 이유는 무엇입니까? (0) | 2020.11.12 |
C ++ 11의 원시 문자열 리터럴 R“(…)”에서 괄호를 사용하는 이유는 무엇입니까? (0) | 2020.11.11 |
테이블의 모든 행을 어떻게 반복 할 수 있습니까? (0) | 2020.11.11 |
파이썬 pdb-루프 건너 뛰기 (0) | 2020.11.11 |