如何在HTML、CSS和JS中点击图像以在模态窗口中显示它

huangapple go评论76阅读模式
英文:

How to display an image clicking on it using modal window on HTML, CSS and JS

问题

在控制台中,你会看到以下错误:

Uncaught TypeError: Cannot set properties of null (setting 'src')
at HTMLImageElement.<anonymous> (seccion1.js:11:22)

这个错误表示在尝试设置图片的 src 属性时出现了问题。问题在于你在JavaScript中使用了错误的ID,应该使用 modal-content 而不是 modalContent。请将以下行更正:

const modalContent = document.getElementById("modal-content");

然后,你的代码应该能够正常工作,以根据所点击的图像显示模态窗口中的图像。

英文:

I'm trying to show an image when I click on it using a modal window, but, when I click on it doesn't display anything. The code is the following:

HTML

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Seccion 1&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;seccion1.css&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;img src=&quot;./Seccion1/pueblodesal2.png&quot; alt=&quot;Imagen central&quot; class=&quot;center-image&quot;&gt;
      &lt;div class=&quot;image-grid&quot;&gt;
        &lt;img src=&quot;./Seccion1/1.jpg&quot; alt=&quot;Imagen 1&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/1.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/2.jpg&quot; alt=&quot;Imagen 2&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/2.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/3.jpg&quot; alt=&quot;Imagen 3&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/3.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/4.jpg&quot; alt=&quot;Imagen 4&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/4.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/5.jpg&quot; alt=&quot;Imagen 5&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/5.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/6.jpg&quot; alt=&quot;Imagen 6&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/6.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/7.jpg&quot; alt=&quot;Imagen 7&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/7.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/8.jpg&quot; alt=&quot;Imagen 8&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/8.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/9.jpg&quot; alt=&quot;Imagen 9&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/9.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/10.jpg&quot; alt=&quot;Imagen 10&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/10.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/11.jpg&quot; alt=&quot;Imagen 11&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/11.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/12.jpg&quot; alt=&quot;Imagen 12&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/12.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/13.jpg&quot; alt=&quot;Imagen 13&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/13.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/14.jpg&quot; alt=&quot;Imagen 14&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/14.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/15.jpg&quot; alt=&quot;Imagen 15&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/15.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/16.jpg&quot; alt=&quot;Imagen 16&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/16.jpg&quot;&gt;
        &lt;img src=&quot;./Seccion1/17.jpg&quot; alt=&quot;Imagen 17&quot; class=&quot;grid-image&quot; data-src=&quot;./Seccion1/17.jpg&quot;&gt;
      &lt;/div&gt;
      
      &lt;div id=&quot;modal&quot; class=&quot;modal&quot;&gt;
        &lt;span class=&quot;close&quot;&gt;&amp;times;&lt;/span&gt;
        &lt;img src=&quot;&quot; alt=&quot;Modal image&quot; class=&quot;modal-content&quot;&gt;
      &lt;/div&gt;
      
    &lt;/div&gt;
    &lt;script src=&quot;seccion1.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt; 

JS

const images = document.querySelectorAll(&quot;.grid-image&quot;);
const modal = document.getElementById(&quot;modal&quot;);
const modalContent = document.getElementById(&quot;modal-content&quot;);
const closeBtn = document.querySelector(&quot;.close&quot;);


images.forEach((image) =&gt; {
  image.addEventListener(&quot;click&quot;, () =&gt; {
    modal.style.display = &quot;block&quot;;
    console.log(image.src)
    modalContent.src = image.src;
  });
});


closeBtn.addEventListener(&quot;click&quot;, () =&gt; {
  modal.style.display = &quot;none&quot;;
});


window.addEventListener(&quot;click&quot;, (event) =&gt; {
  if (event.target == modal) {
    modal.style.display = &quot;none&quot;;
  }
}); 

In the console I get this error:

Uncaught TypeError: Cannot set properties of null (setting &#39;src&#39;)
at HTMLImageElement.&lt;anonymous&gt; (seccion1.js:11:22)

I try to debug the image.src and it gets the correct path. I'd like to know how to display the image using the src at class modal-content.

答案1

得分: 2

你的HTML中没有#modal-content

英文:

You don't have #modal-content in html.

&lt;img src=&quot;&quot; alt=&quot;Modal image&quot; id=&quot;modal-content&quot; class=&quot;modal-content&quot;&gt;

答案2

得分: 1

在你的HTML代码中,modal-content 是一个类(class),而不是一个id,所以当你调用 getElementById 时,它会返回 null。只需给 modal-content 元素添加一个id。

<img src="" alt="模态框图片" class="modal-content" id="modal-content">
英文:

In your HTML code, modal-content is a class, not an id, so when you call getElementById, that returns null. Just add an id to the modal-content element.

&lt;img src=&quot;&quot; alt=&quot;Modal image&quot; class=&quot;modal-content&quot; id=&quot;modal-content&quot;&gt;

huangapple
  • 本文由 发表于 2023年3月1日 09:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定