亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我的 javascript 函數無法正常工作

我的 javascript 函數無法正常工作

慕桂英3389331 2022-11-03 10:12:26
我正在做一個網站項目。我在 w3schools 中看到了圖像動畫。我已經試過了,它只適用于一張照片——// Get the modalvar modal = document.getElementById('myModal');// Get the image and insert it inside the modalvar img = document.getElementById('a');var modalImg = document.getElementById("img01");img.onclick = function() {  modal.style.display = "block";  modalImg.src = this.src;}// Get the <span> element that closes the modalvar span = document.getElementsByClassName("close")[0];// When the user clicks on <span> (x), close the modalspan.onclick = function() {  modal.style.display = "none";}但我想將它用于多個圖像。所以我創建了一個這樣的函數:function myBeautifulFunc(imageId, modalImageId) {  var modal = document.getElementById('myModal');  var img = document.getElementById(imageId);  var modalImg = document.getElementById(modalImageId);    img.onclick = function() {    modal.style.display = "block";    modalImg.src = this.src;  }  // 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.style.display = "none";  }}并調用了這個函數:myBeautifulFunc('a', 'img01')結果是:它適用于第一張圖片,但是當我在第二張嘗試時,它看起來像這樣:https ://ibb.co/Mk8Xw1Z當我點擊它時,我只想顯示一張圖片,但它顯示了兩張圖片。我怎么解決這個問題?
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

您的關閉功能span.onclick = function()不會隱藏子項,因此第二次單擊時,您會看到上次單擊時的上一個圖像和 modalImage。


function myBeautifulFunc(imageId, modalImageId) {


  var modal = document.getElementById('myModal');


  var img = document.getElementById(imageId);

  var modalImg = document.getElementById(modalImageId);

  


  img.onclick = function() {

    modal.style.display = "block";

    modalImg.src = this.src;

  }


  // 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.style.display = "none";

    img.style.display = "none"         // hide this also

    modalImg.style.display = "none"     // hide this also

  }

}

在任何點擊之前

  • .myModal = 隱藏

  • img1 = 隱藏

  • modalImg1 = 隱藏

第一次點擊后關閉

  • .myModal = 隱藏

  • img1 = 可見

  • modalImg1 = 可見

第二次點擊后

  • .myModal = 隱藏

  • img1 = 可見

  • modalImg1 = 可見

  • img2 = 可見

  • modalImg2 = 可見


查看完整回答
反對 回復 2022-11-03
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號