私たちが実行するDrupalサイトのJavascriptを書こうとしています。理想的には、これはDOMクラスが使用されるたびにすべてのページで実行されます。これは私がこれまで持っているものです。モーダル・アレイとforループ
window.onload = function() {
// Get the modal
var modal = ["m1", "m2", "m3", "m4", "m5"];
for (var i = 0; i < modal.length; i++){
document.getElementById(modal[i]);
}
// Get the button that opens the modal
var btn = ["btn1", "btn2", "btn3", "btn4", "btn5", "btn6"];
for (var i = 0; i < btn.length; i++){
document.getElementById(btn[i]);
}
// When the user clicks on the button, open the modal
for (var i = 0; i < btn.length; i++)
{
btn[i].onclick = function() {
modal[i].style.display = "block";
}
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal[i].style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal[i].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: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
}
/* 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;
}
<!-- Trigger/Open The Modal -->
<a id="myBtn">Open Me</a>
<!-- The Modal -->
<div id="modal" class="modal">
<div class="modal-content drive">
<span class="close">x</span>
<h2>Foo</h2>
<p>Random Info</p>
<p><a class="btn" href="/bar">Foo Bar</a></p>
</div>
for (i = 0, i < btn.length; i++) <----This parenthesis
{
btn[i].onclick = function()
modal[i].style.display = "block";
}
感謝を構文エラーを取得していますのよう
! 編集:壊れたJSを修正しましたが、モーダルはポップアップしません。何か案は?
うんのライン77上の構文エラーだった、私はそれらを修正しました。しかし、私はタグを使ってモーダルを開くことはできません。変数はタグで使用されるIDなので、GetElementByTagNameに切り替える必要がありますか? – Lastweek
あなたが何をしようとしているのか分かりません –
私の元の投稿ではっきりしないと申し訳ありません。私はモーダルを作成しようとしています。リンクや画像がクリックされたときに表示されるWindows。 – Lastweek