2017-02-11 14 views
0

ユーザーがモーダル外のどこかをクリックすると閉じるWebページに2つのポップアップモーダルがあります。問題は第1の作品ではない第2の作品だけです。Javaスクリプトのデュアルモーダルを閉じることができません

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

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

答えて

0

両方のポップアップモーダルで同じクリック機能を使用します。

window.onclick = function(event){ 
if (event.target == modal1)  
{ modal1.style.display = "none"; } 
if (event.target == modal2) 
{ modal2.style.display = "none"; } 
} 
0

第1のオンクリックを第2のオンクリックで上書きしています。

そして、モーダルタイプをチェックし、設定したベースをチェックするウィンドウ上のonclickが1つだけ表示されます。

+0

私はコードをオーバーライドしています。しかし、私は2つの変数modal1とmodal2を持っている、私は両方のモーダルを閉じなければならないのですか?コードスニペットを提供できますか? –

0

最初のonclickを2番目のonclickで上書きしています。このようにすることができます。

window.onclick = function(event){ 
if (event.target == modal1){modal1.style.display = "none"; } 
if (event.target == modal2){modal2.style.display = "none";} 
} 
関連する問題