javascriptでモーダルウィンドウを作成しています。
小さな写真を用意してクリックしたら写真が大きくなりdisplay=block、×と後ろオーバレイをクリックしたらdisplay=noneで閉じるというよくある実装をしています。
しかし実装はできたのですが他のページへ移行できなくなりました。
恐らくe.preventDefault()のせいだと思うのですがこの後にどうゆう処理をしたらいいのか分かりません。
教えていただけませんか?
html
1 <div id="modal"> 2 <div class="modal-content f_wrap"> 3 <a href="#" id="modal_close">×</a> 4 <img src="images/img.jpg" width="500px" height="400px" alt="スケジュール"> 5 </div> 6 </div> 7 <p id="modal_open"> 8 <a href=""> 9 <img src="images/img.jpg" width="200px" height="200px" alt="スケジュール"> 10 </a> 11 </p> 12
css
1div#modal { 2 display: none; 3 z-index: 1; 4 position: fixed; 5 top: 0; 6 left: 0; 7 right: 0; 8 bottom: 0; 9 height: 100%; 10 width: 100%; 11 overflow: auto; 12 background-color: rgba(0,0,0,0.5); 13} 14 15div#modal div.modal-content { 16 width: 600px; 17 height: 500px; 18 position: absolute; 19 top: 100px; 20 left: calc(50% - 300px); 21 background-color: white; 22 justify-content: center; 23 align-items: center; 24} 25 26div.modal-content a { 27position: absolute; 28right: 10px; 29top: 0px; 30font-size: 40px; 31font-weight: bold; 32} 33
javascript
1 var btn = document.getElementById('modal_open'); 2 var btn_close = document.getElementById('modal_close'); 3 var modal = document.getElementById('modal'); 4 5 btn.addEventListener('click',function() { 6 modal.style.display = 'block'; 7 }); 8 9 btn_close.addEventListener('click',function() { 10 modal.style.display = 'none'; 11 }); 12 13 window.addEventListener('click',function(e) { 14 e.preventDefault(); 15 if(e.target == modal) { 16 modal.style.display = 'none'; 17 } 18 }); 19
e.preventDefault(); を外すのではダメなのですか?
回答3件
あなたの回答
tips
プレビュー