2017-11-15 3 views
0

学校のプロジェクトでは、私のクラスのすべての生徒にモーダルを作成したいと思います。私の問題は、最初のモーダルしか働いていないことです。あなたのコードから模型をコピーする方法

// 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 on 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"; 
 
    } 
 
}
/* The Modal (background) */ 
 

 
.modal { 
 
    display: none; 
 
    /* Hidden by default */ 
 
    position: fixed; 
 
    /* Stay in place */ 
 
    z-index: 1; 
 
    /* Sit on top */ 
 
    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/Box */ 
 

 
.modal-content { 
 
    background-color: #fefefe; 
 
    margin: 14% auto; 
 
    /* 15% from the top and centered */ 
 
    padding: 20px; 
 
    border: none; 
 
    width: 80%; 
 
    /* Could be more or less, depending on screen size */ 
 
    box-shadow: 0 10px 20px 0px rgba(0, 0, 0, 0.5); 
 
} 
 

 

 
/* The Close Button */ 
 

 
.close { 
 
    color: #aaa; 
 
    float: right; 
 
    font-size: 28px; 
 
    font-weight: bold; 
 
} 
 

 
.close:hover, 
 
.close:focus { 
 
    color: black; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
} 
 

 

 
/* Button Design */ 
 

 
#myBtn { 
 
    background-image: url(/images/blank-profile-picture-973460_960_720.png); 
 
    font-family: Lato-Bold; 
 
    font-size: 20px; 
 
    background-color: rgb(55, 154, 177); 
 
    background-size: cover; 
 
    width: 300px; 
 
    height: 300px; 
 
    margin: 10px; 
 
    float: left; 
 
    text-align: center; 
 
    color: white; 
 
    border: 0; 
 
    cursor: pointer; 
 
} 
 

 
p.Info { 
 
    font-family: Lato-regular; 
 
    font-size: 15px; 
 
    color: rgb(40, 40, 40); 
 
}
<!-- Trigger/Open The Modal --> 
 
<button id="myBtn">Anthony</button> 
 

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

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <span class="close">&times;</span> 
 
    <p class="Info"> 
 
     Nachname:König<br> Vorname:Anthony 
 
     <br> Geburtstag:24.12.1998 
 
     <br> Herkunft:Deutschland 
 
     <br> Adresse:xxxxxxxxxxxxxxxxx 
 
     <br> Mobilnummer:xxxxxxxxxxxxxx 
 
     <br> E-Mail:xxxxxxxxxxxxxxxxxbr> Fachlicher Schwerpunkt:Programmieren<br> Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br> Socialmedia:url Facebook: url Instagram:<br> Snapcode: anthony.k1ng<br> 
 
    </p> 
 
    </div> 
 

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

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

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <span class="close">&times;</span> 
 
    <p class="Info"> 
 
     Nachname:König<br> Vorname:Anthony 
 
     <br> Geburtstag:24.12.1998 
 
     <br> Herkunft:Deutschland 
 
     <br> Adresse:xxxxxxxxxxxxxxxxxxxxxx 
 
     <br> Mobilnummer:xxxxxxxxxxxxxxxxxx 
 
     <br> E-Mail:xxxxxxxxxxxxxxxx 
 
     <br> Fachlicher Schwerpunkt:Programmieren<br> Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br> Socialmedia:url Facebook: url Instagram:<br> Snapcode: anthony.k1ng<br> 
 
    </p> 
 
    </div> 
 

 
</div>

答えて

0

、次の2つのモーダルに同じid myModalを与えました。 htmlでは、Idはページ上に1回しか存在できません。そのIdが存在する最初のモーダルは、同じことがあなたのボタンにも当てはまるのでトリガされる唯一のものです。 ボタンとモーダルの個々のIDを変更し、クリックされたボタンに基づいてそれぞれをターゲットにします。同じことが、この

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

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

<!-- Modal content --> 
<div class="modal-content"> 
<span class="close" id="close1">&times;</span> 
<p class="Info"> 
Nachname:König<br> 
Vorname:Anthony<br> 
Geburtstag:24.12.1998<br> 
Herkunft:Deutschland<br> 
Adresse:xxxxxxxxxxxxxxxxx<br> 
Mobilnummer:xxxxxxxxxxxxxx<br> 
E-Mail:xxxxxxxxxxxxxxxxxbr> 
Fachlicher Schwerpunkt:Programmieren<br> 
Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br> 
Socialmedia:url Facebook: url Instagram:<br> 
Snapcode: anthony.k1ng<br> 
</p> 
</div> 

</div> 
<!-- Trigger/Open The Modal --> 
<button id="myBtn2" class="myBtn">David</button> 

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

<!-- Modal content --> 
<div class="modal-content"> 
<span class="close" id="close2">&times;</span> 
<p class="Info"> 
Nachname:König<br> 
Vorname:Anthony<br> 
Geburtstag:24.12.1998<br> 
Herkunft:Deutschland<br> 
Adresse:xxxxxxxxxxxxxxxxxxxxxx<br> 
Mobilnummer:xxxxxxxxxxxxxxxxxx<br> 
E-Mail:xxxxxxxxxxxxxxxx<br> 
Fachlicher Schwerpunkt:Programmieren<br> 
Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br> 
Socialmedia:url Facebook: url Instagram:<br> 
Snapcode: anthony.k1ng<br> 
</p> 
</div> 

</div> 

にこの

var modal1 = document.getElementById('myModal1'); 
var modal2 = document.getElementById('myModal2'); 
// Get the button that opens the modal 
var btn1 = document.getElementById("myBtn1"); 
var btn2 = document.getElementById("myBtn2"); 

// Get the <span> element that closes the modal 
var span1 = document.getElementById("close1"); 
var span2 = document.getElementById("close2"); 
// When the user clicks on the button, open the modal 
btn1.onclick = function() { 
    modal1.style.display = "block"; 
} 
btn2.onclick = function() { 
    modal2.style.display = "block"; 
} 
// When the user clicks on <span> (x), close the modal 
span1.onclick = function() { 
    modal1.style.display = "none"; 
} 
span2.onclick = function() { 
    modal2.style.display = "none"; 
} 
// When the user clicks anywhere outside of the modal, close it 
window.onclick = function(event) { 
    if (event.target == modal1) { 
     modal1.style.display = "none"; 
    } 
    if (event.target == modal2) { 
     modal2.style.display = "none"; 
    } 
} 

し、HTMLにあなたのJavaScriptを変更し、CSS /*モーダル(バックグラウンド)

もあなたの閉じるボタンのために行われるべきです*/

.modal { 
    display: none; /* Hidden by default */ 
    position: fixed; /* Stay in place */ 
    z-index: 1; /* Sit on top */ 
    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/Box */ 
.modal-content { 
    background-color: #fefefe; 
    margin: 14% auto; /* 15% from the top and centered */ 
    padding: 20px; 
    border: none; 
    width: 80%; /* Could be more or less, depending on screen size */ 
     box-shadow: 0 10px 20px 0px rgba(0, 0, 0, 0.5); 
} 

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

.close:hover, 
.close:focus { 
    color: black; 
    text-decoration: none; 
    cursor: pointer; 
} 
/* Button Design */ 
.myBtn { 
    background-image: url(/images/blank-profile-picture-973460_960_720.png); 
    font-family: Lato-Bold; 
    font-size: 20px; 
    background-color: rgb(55, 154, 177); 
    background-size:cover; 
    width: 300px; 
    height: 300px; 
    margin: 10px; 
    float: left; 
    text-align: center; 
    color:white; 
    border:0; 
    cursor: pointer; 
} 

p.Info { 
    font-family: Lato-regular; 
    font-size: 15px; 
    color: rgb(40, 40, 40); 


} 
+0

私は、CSS/JavaScriptのaswellで何かを変更する必要がありますか? –

+0

@SphexOw ... jsのフィドルでコードを更新してもいいですか? – UniQue

+0

@UniQue確かに:)すごくいいですか、今すぐ自分で試してみてください! –

1

ここで、より一般的なアプローチは、 dom要素のrdering。

この場合、さらに追加したい場合は、ボタンとモーダルhtmlをコピーして内容を変更するだけです。コード、CSS、またはdivのIDを変更する必要はありません。

JSFiddle

// Get the modals 
 
var modals = document.getElementsByClassName('myModal'); 
 

 
// Get the buttons that opens the modals 
 
var btns = document.getElementsByClassName("myBtn"); 
 

 
// Get the <span> elements that closes the modals 
 
var spans = document.getElementsByClassName("close"); 
 

 
function makeShower(index) { 
 
\t return function(event) { 
 
    \t modals[index].style.display = "block"; 
 
    } 
 
} 
 

 
function makeHider(index) { 
 
\t return function(event) { 
 
    \t modals[index].style.display = "none"; 
 
    } 
 
} 
 

 
for (let i = 0; i < btns.length; i++) { 
 
    btns[i].onclick = makeShower(i); 
 
    spans[i].onclick = makeHider(i); 
 
} 
 

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target.className == "myModal modal") { 
 
    event.target.style.display = "none"; 
 
    } 
 
}
/* The Modal (background) */ 
 
.modal { 
 
    display: none; /* Hidden by default */ 
 
    position: fixed; /* Stay in place */ 
 
    z-index: 1; /* Sit on top */ 
 
    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/Box */ 
 
.modal-content { 
 
    background-color: #fefefe; 
 
    margin: 14% auto; /* 15% from the top and centered */ 
 
    padding: 20px; 
 
    border: none; 
 
    width: 80%; /* Could be more or less, depending on screen size */ 
 
     box-shadow: 0 10px 20px 0px rgba(0, 0, 0, 0.5); 
 
} 
 

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

 
.close:hover, 
 
.close:focus { 
 
    color: black; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
} 
 
/* Button Design */ 
 
.myBtn { 
 
    background-image: url(/images/blank-profile-picture-973460_960_720.png); 
 
    font-family: Lato-Bold; 
 
    font-size: 20px; 
 
    background-color: rgb(55, 154, 177); 
 
    background-size:cover; 
 
    width: 300px; 
 
    height: 300px; 
 
    margin: 10px; 
 
    float: left; 
 
    text-align: center; 
 
    color:white; 
 
    border:0; 
 
    cursor: pointer; 
 
} 
 

 
p.Info { 
 
    font-family: Lato-regular; 
 
    font-size: 15px; 
 
    color: rgb(40, 40, 40); 
 

 

 
}
<!-- Trigger/Open The Modal --> 
 
<button class="myBtn">Anthony</button> 
 

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

 
<!-- Modal content --> 
 
<div class="modal-content"> 
 
<span class="close">&times;</span> 
 
<p class="Info"> 
 
Nachname:König<br> 
 
Vorname:Anthony<br> 
 
Geburtstag:24.12.1998<br> 
 
Herkunft:Deutschland<br> 
 
Adresse:xxxxxxxxxxxxxxxxx<br> 
 
Mobilnummer:xxxxxxxxxxxxxx<br> 
 
E-Mail:xxxxxxxxxxxxxxxxxbr> 
 
Fachlicher Schwerpunkt:Programmieren<br> 
 
Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br> 
 
Socialmedia:url Facebook: url Instagram:<br> 
 
Snapcode: anthony.k1ng<br> 
 
</p> 
 
</div> 
 

 
</div> 
 
<!-- Trigger/Open The Modal --> 
 
<button class="myBtn">David</button> 
 

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

 
<!-- Modal content --> 
 
<div class="modal-content"> 
 
<span class="close">&times;</span> 
 
<p class="Info"> 
 
Nachname:David<br> 
 
Vorname:David<br> 
 
Geburtstag:24.12.1998<br> 
 
Herkunft:Deutschland<br> 
 
Adresse:xxxxxxxxxxxxxxxxxxxxxx<br> 
 
Mobilnummer:xxxxxxxxxxxxxxxxxx<br> 
 
E-Mail:xxxxxxxxxxxxxxxx<br> 
 
Fachlicher Schwerpunkt:Programmieren<br> 
 
Hobbys:Basketball, Tennis, Krafttraining, Gaming und Fussball.<br> 
 
Socialmedia:url Facebook: url Instagram:<br> 
 
Snapcode: anthony.k1ng<br> 
 
</p> 
 
</div> 
 

 
</div>

関連する問題