いつもお世話になっております。
また一つつまづいたので、質問させてください。
実現したいこと
インフォーメーションを設定しており、クリックした内容をポップアップで表示させています。
その時に、背後をスクロール禁止にしたいです。
class="info-ttl"をクリックしたときに、class="info-content"にクラス名activeが追加で付くように設定しています。
該当のソースコード
html
1<body> 2<dl> 3 <dt>2022.07.14</dt> 4 <dd> 5 <span class="info-ttl">インフォメーションのタイトル</span> 6 <div class="info-content">ポップアップで表示させてる内容</div> 7 </dd> 8 <dt>2022.07.14</dt> 9 <dd> 10 <span class="info-ttl">インフォメーションのタイトル</span> 11 <div class="info-content">ポップアップで表示させてる内容</div> 12 </dd> 13 <dt>2022.07.14</dt> 14 <dd> 15 <span class="info-ttl">インフォメーションのタイトル</span> 16 <div class="info-content">ポップアップで表示させてる内容</div> 17 </dd> 18</dl> 19</body>
試したこと
javascript
1const infoTtl = document.querySelectorAll('.info-ttl'); 2const scrollProhabited = document.querySelectorAll('.info-content'); 3const scrollProhabitedBody = document.getElementsByTagName('body'); 4 5infoTtl.forEach(function (info) { 6 info.addEventListener('click', function () { 7 scrollProhabited.forEach(function (element) { 8 if (element.classList.contains('active')) { 9 scrollProhabitedBody.classList.add('modal-open'); 10 } else { 11 scrollProhabitedBody.classList.remove('modal-open'); 12 } 13 }); 14 }); 15}); 16
出てるエラー
クリック時にremove部分でエラーが出ます。
Cannot read properties of undefined (reading 'remove')
補足
console.logで確認したところ、そもそもクリック時にelement.classList.contains('active')がtrueになってないようです。
よろしくお願いいたします。
追記
ポップアップの設定は以下のように行っています。
javascript
1/*--Informationのポップアップ--*/ 2const popupTtl = document.querySelectorAll('.tinfo-ttl'); 3const popupBox = document.querySelectorAll('.info-content); 4const popupCloseBtn = document.querySelectorAll( 5 '.top-info-content-popup-close-btn' 6); 7const popupContentBg = document.querySelectorAll( 8 '.top-info-content-popup-inner-box' 9); 10 11window.addEventListener('DOMContentLoaded', function () { 12 for (let i = 0; i < popupTtl.length; i++) { 13 popupTtl[i].addEventListener('click', function (e) { 14 e.preventDefault(); 15 let dataPopupTtl = popupTtl[i].getAttribute('data-popup-ttl'); 16 for (let j = 0; j < popupBox.length; j++) { 17 if (popupBox[j].getAttribute('data-popup-cont') === dataPopupTtl) { 18 popupBox[j].classList.add('active'); 19 } 20 } 21 }); 22 popupContentBg[i].addEventListener('click', function () { 23 popupBox[i].classList.add('active2'); 24 setTimeout(function () { 25 popupBox[i].classList.remove('active'); 26 popupBox[i].classList.remove('active2'); 27 }, 300); 28 }); 29 popupCloseBtn[i].addEventListener('click', function () { 30 popupBox[i].classList.add('active2'); 31 setTimeout(function () { 32 popupBox[i].classList.remove('active'); 33 popupBox[i].classList.remove('active2'); 34 }, 300); 35 }); 36 } 37}); 38 39//Informationポップアップを開いてる時にスクロール禁止 40const scrollProhabitedBody = document.getElementsByTagName('body'); 41 42popupTtl.forEach(function (info) { 43 info.addEventListener('click', function () { 44 popupBox.forEach(function (element) { 45 if (element.classList.contains('active')) { 46 scrollProhabitedBody.classList.add('modal-open'); 47 } else { 48 scrollProhabitedBody.classList.remove('modal-open'); 49 } 50 }); 51 }); 52}); 53
回答2件
あなたの回答
tips
プレビュー