2016-10-18 6 views
0

私たちが実行する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を修正しましたが、モーダルはポップアップしません。何か案は?

答えて

0

あなたがforループのスコープで変数を宣言していない:、

for (i = 0, i < btn.length; i++) 

、次の行の関数が間違っている、mulstacheが必要です

btn[i].onclick = function() 

正しい:

for (var i = 0; i < btn.length; i++) 
{ 
    btn[i].onclick = function(){ 
     modal[i].style.display = "block"; 
    } 
} 
+0

あなたが何をしようとしているのか分かりません –

+0

私の元の投稿ではっきりしないと申し訳ありません。私はモーダルを作成しようとしています。リンクや画像がクリックされたときに表示されるWindows。 – Lastweek

0

これはコメントであったはずですが、私のコメントは無効なので、私は答える必要がありました。 このコメント行を見つける:あるべき

for (var i = 0, i < btn.length; i++) 

// When the user clicks on the button, open the modal 

を右そのコメントの下に、あなたのforループは、このようなものです

for (var i = 0; i < btn.length; i++) 

あなただけの後にセミコロン;を逃しましたvar i = 0とし、代わりにコンマ,を追加しました。

慎重にエラーメッセージを読んで、それは私がばかなんだ言及したファイルhttp://stacksnippets.net/js