简体   繁体   English

如何将 Javascript 函数应用于一个类的多个实例?

[英]How do I apply Javascript functions to multiple instances of a class?

I have a web page that is displaying a set of images using a table for simple layout.我有一个网页,它使用表格来显示一组图像以进行简单的布局。 What I'd like to do is allow a user to click the image and have that image appear magnified in a modal.我想做的是允许用户单击图像并在模态中放大该图像。

The html row holding a graphic:包含图形的 html 行:

<tr>
    <td>Graphic One</td>
    <td><img class="graph" src="/chart1.jpg">
        <div class="modal">
        <div class="modal-content">
            <span class="close-btn">&times;</span>
                <img src="/chart1.jpg">
        </div>
    </div>
    </td>
</tr>

This is repeated to create more rows with graphics.重复此操作以创建更多带有图形的行。 Here's the script:这是脚本:

<script>
let modalBtn = document.querySelector(".graph")
let modal = document.querySelector(".modal")
let closeBtn = document.querySelector(".close-btn")
    modalBtn.onclick = function(){
    modal.style.display = "block"
    }
closeBtn.onclick = function(){
    modal.style.display = "none"
    }
    window.onclick = function(e){
    if(e.target == modal){
        modal.style.display = "none"
    }
}
</script>

This only seems to activate the first graphic on the page, though, so I'm assuming that the querySelector() stops at the first instance found.不过,这似乎只会激活页面上的第一个图形,所以我假设 querySelector() 在找到的第一个实例处停止。

How can I apply this to every row with an image in the table?如何将其应用于表中带有图像的每一行?

If you want to apply it to every table row, all you need is to select all the <td> and then iteratively apply the same logic as you have above to the elements found inside it.如果你想把它应用到表格的每一行,你只需要选择所有的<td>然后迭代地将与上面相同的逻辑应用到它里面的元素。

To access the individual elements, you can simply use Element.querySelector() instead, assuming Element refers to the <td> .要访问单个元素,您可以简单地使用Element.querySelector()代替,假设Element指的是<td> It helps to give this <td> element that contains all these elements a class, so you can narrow down the selection, eg <td class="action"> :给这个包含所有这些元素的<td>元素一个类是有帮助的,这样你就可以缩小选择范围,例如<td class="action">

<tr>
    <td>Graphic One</td>
    <td class="action">
        <img class="graph" src="/chart1.jpg">
        <div class="modal">
        <div class="modal-content">
            <span class="close-btn">&times;</span>
                <img src="/chart1.jpg">
        </div>
    </div>
    </td>
</tr>

...then you can do this: ...然后你可以这样做:

document.querySelectorAll('.action').forEach(el => {
    let modalBtn = el.querySelector(".graph");
    let modal = el.querySelector(".modal");
    let closeBtn = el.querySelector(".close-btn");

    modalBtn.addEventListener('click', e => {
        // Stop click event from bubbling up
        e.stopPropagation();

        modal.style.display = 'block';
    });

    closeBtn.addEventListener('click', e => {
        // Stop click event from bubbling up
        e.stopPropagation();

        modal.style.display = 'none';
    });
});

// Listen to click event on the window ONCE, outside the forEach loop
window.addEventListener('click', () => {
    document.querySelectorAll(".modal").forEach(el => {
        el.style.display = 'none';
    });
});

You can see that I have left out the window click event listener in the for each loop.您可以看到我在 for each 循环中忽略了窗口单击事件侦听器。 The reason is that you can simply rely on event.stopPropagation to stop the click event from bubbling to the window object.原因是你可以简单地依靠event.stopPropagation来阻止点击事件冒泡到 window 对象。 That means, you simply have to bind the click event once on the window.这意味着,您只需在窗口上绑定一次单击事件。

Also, I would not suggest using Element.onclick = Function to add event listeners, because that means you can only assign one handler this way.此外,我不建议使用Element.onclick = Function添加事件侦听器,因为这意味着您只能以这种方式分配一个处理程序。 Use Element.addEventListener instead.改用Element.addEventListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM