2017-04-19 1 views
0

모달 팝업을 여러 개 만들고 싶습니다. 아래 코드는 한 팝업에 대해 작동하지만 어떻게 3 가지 팝업에 대해 수행합니까 ?? 난 그냥 당신은 각각의 고유 ID를 모달 따라 버튼을 결합 줄 필요가 휴대 전화여러 모달 팝업

<!DOCTYPE html> 
<html> 
<head> 
<style> 
/* The Modal (background) */ 
.modal { 
display: none; /* Hidden by default */ 
position: fixed; /* Stay in place */ 
z-index: 1; /* Sit on top */ 
padding-top: 100px; /* Location of the box */ 
left: 0; 
top: 0; 
width: 100%; /* Full width */ 
height: 100%; /* Full height */ 
overflow: auto; /* Enable scroll if needed */ 
background-color: rgb(0,0,0); /* Fallback color */ 
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ 
} 

/* Modal Content */ 
.modal-content { 
background-color: #fefefe; 
margin: auto; 
padding: 20px; 
border: 1px solid #888; 
width: 80%; 
} 

/* The Close Button */ 
.close { 
color: #aaaaaa; 
float: right; 
font-size: 28px; 
font-weight: bold; 
} 

.close:hover, 
.close:focus { 
color: #000; 
text-decoration: none; 
cursor: pointer; 
} 
</style> 
</head> 
<body> 

<h2>Test</h2> 

<!-- Trigger/Open The Modal --> 
<button id="myBtn">Open Modal</button> 

<!-- The Modal --> 
<div id="myModal" class="modal"> 

<!-- Modal content --> 
<div class="modal-content"> 
<span class="close">&times;</span> 
<p>Some text in the Modal..</p> 
</div> 

</div> 

<!-- Trigger/Open The Modal --> 
<button id="myBtn">Open Another</button> 

<!-- The Modal --> 
<div id="myModal" class="modal"> 

<!-- Modal content --> 
<div class="modal-content"> 
<span class="close">&times;</span> 
<p>Some Other Text..</p> 
</div> 

</div> 
<script> 
// Get the modal 
var modal = document.getElementById('myModal'); 

// Get the button that opens the modal 
var btn = document.getElementById("myBtn"); 

// Get the <span> element that closes the modal 
var span = document.getElementsByClassName("close")[0]; 

// When the user clicks the button, open the modal 
btn.onclick = function() { 
modal.style.display = "block"; 
} 

// When the user clicks on <span> (x), close the modal 
span.onclick = function() { 
modal.style.display = "none"; 
} 

// When the user clicks anywhere outside of the modal, close it 
window.onclick = function(event) { 
if (event.target == modal) { 
    modal.style.display = "none"; 
} 
} 
</script> 

</body> 
</html> 
+0

HTML을 보는 것도 도움이 될 것입니다. CSS에는 사용자가 세 개를 가질 수 없도록하는 요소는 없습니다. "모달"클래스를 가진 각각의 3 개 div는 3 개 모달을 만들어야합니다. – isick

+0

업데이트 된 코드 .. 시도해 볼 수는 있지만 작동하지 않습니다 .. –

답변

0

를 사용하여 ... https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal 분명히 전체 코드를 붙여되지 않습니다 .. 배우는 중이에요. 중복 변경 될 필요가있는 세 가지

  • 버튼 ID ID = "myBtn" = "myBtn2"
  • 모달 이드 까지입니다 ID = "myModal"을에 id = "myModal2"
  • javacript는 id로 버튼과 모달을 선택합니다. document.getElementById ('myModal');

지금은 두 개의 modals가 동일하고 id가 고유해야합니다. 따라서 두 번째 모달을 원한다면 (그리고 id를 통해 JavaScript에서 모달과 상호 작용할 것으로 예상하는 경우) 두 번째 모달에 고유 한 ID를 할당해야합니다.

+0

나는 이미 그걸 시도해 보았지만 작동하지 않는 것 같아요. 문제는 자바 스크립트와 솔직히 관련이 있다고 생각합니다. –

+0

@SemajRellim 자바 스크립트가 아닙니다. 왜냐하면 나는 당신을 위해 그것을 여기에서했기 때문에 https://jsfiddle.net/Lnq6gtLb/이 코드를 개선 할 수있는 많은 방법이 있습니다. 나는 당신이 개발자로서 향상시킬 수 있기 때문에 내가했던 것을 해부하고 그것을보다 효율적으로 만드는 방법을 생각하면서 시간을 보내길 권한다. 행운을 빌어 요. – isick

+0

감사합니다. 많은 isick과 예. 도움이됩니다. 그리고 더 많은 것을 배우기 위해 더 많이 플레이 할 것입니다. –