<html>
<head>
	<meta charset="utf-8">
	<title>javascript</title>
	<style>
	/* Style the buttons that are used to open and close the accordion panel */
	.accordion {
	  background-color: #eee;
	  color: #444;
	  cursor: pointer;
	  padding: 18px;
	  width: 100%;
	  text-align: left;
	  border: none;
	  outline: none;
	  transition: 0.4s;
	}
	
	/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
	.active, .accordion:hover {
	  background-color: #ccc;
	}
	
	/* Style the accordion panel. Note: hidden by default */
	.panel {
	  padding: 0 18px;
	  background-color: white;
	  display: none;
	  overflow: hidden;
	}
	</style>	
	
</head>
<body>
		<button class="accordion">Section 1</button>
		<div class="panel">
		  <p>Lorem ipsum...</p>
		</div>
		
		<button class="accordion">Section 2</button>
		<div class="panel">
		  <p>Lorem ipsum...</p>
		</div>
		
		<button class="accordion">Section 3</button>
		<div class="panel">
		  <p>Lorem ipsum...</p>
		</div>
</body>
<script>
		var acc = document.getElementsByClassName("accordion"); //아코디언클래스리스트를 가져온다.
		var i;
		
		for (i = 0; i < acc.length; i++) {
		  acc[i].addEventListener("click", function() { //클릭이벤트를 추가한다. 
		    /* Toggle between adding and removing the "active" class,
		    to highlight the button that controls the panel */
		    this.classList.toggle("active"); //클래스를 추가하거나 삭제함. 
		
		    /* Toggle between hiding and showing the active panel */
		    var panel = this.nextElementSibling; //현재 아코디언의 다음노트를 가져온다. panel이 옴 
		    if (panel.style.display === "block") { //출력모드가 블럭인지 none인지 체크한다.
		      panel.style.display = "none";
		    } else {
		      panel.style.display = "block";
		    }
		  });
		}
	</script>
</html>

이번소스는 아코디언이에요. 

아코디언

var acc = document.getElementsByClassName("accordion"); //아코디언클래스리스트를 가져온다.

acc갯수 만큼 for문을 돌려요. 

클릭이벤트를 추가해주면 되요.

 acc[i].addEventListener("click", function() { //클릭이벤트를 추가한다. 
		    /* Toggle between adding and removing the "active" class,
		    to highlight the button that controls the panel */
		    this.classList.toggle("active"); //클래스를 추가하거나 삭제함. 
		
		    /* Toggle between hiding and showing the active panel */
		    var panel = this.nextElementSibling; //현재 아코디언의 다음노트를 가져온다. panel이 옴 
		    if (panel.style.display === "block") { //출력모드가 블럭인지 none인지 체크한다.
		      panel.style.display = "none";
		    } else {
		      panel.style.display = "block";
		    }
		  });

https://www.w3schools.com/ 여기사이트 소스를 참고했어요.

 

 

W3Schools Online Web Tutorials

HTML Example:

This is a heading

This is a paragraph.

Try it Yourself » CSS Example: body {   background-color: lightblue; } h1 {   color: white;   text-align: ce

 

www.w3schools.com

 

 

W3Schools Online Web Tutorials

HTML Example:

This is a heading

This is a paragraph.

Try it Yourself » CSS Example: body {   background-color: lightblue; } h1 {   color: white;   text-align: ce

 

www.w3schools.com

 

 

블로그 이미지

은호아빠

여행, 맛집, 일상, 프로그래밍, 개발자, 윈도우, 웹, jsp, spring, db, mysql, oracle, c#

,