简体   繁体   English

Javascript如何为从PHP foreach循环创建的一系列按钮创建切换按钮

[英]Javascript how to create a toggle button for a series of buttons created from a PHP foreach loop

I have a gallery program that has buttons on each of the images. 我有一个图库程序,在每个图像上都有按钮。 Each button is meant to open a menu, and it is the same menu for every image (which is a simple list of folders to move the image to). 每个按钮用于打开一个菜单,并且每个图像都是相同的菜单(这是将图像移动到的文件夹的简单列表)。 I have a PHP foreach loop that is able to assign a different js function name to each of the buttons, in the efforts to be able to have a toggle button for each image. 我有一个PHP foreach循环,能够为每个按钮分配一个不同的js函数名称,从而能够为每个图像设置一个切换按钮。

I'm hoping somebody can illustrate how to implement a better solution for this, rather than having the foreach loop assigning a different function name like this: <button onclick="function1()"></button> , <button onclick="function2()"></button> , and so on. 我希望有人能够说明如何为此实现更好的解决方案,而不是让foreach循环分配这样的不同函数名称: <button onclick="function1()"></button><button onclick="function2()"></button> ,依此类推。 This is because there could be up to 200 different images and this would require up to 200 different javascript functions to be written. 这是因为可能会有多达200个不同的图像,并且这将需要编写多达200个不同的javascript函数。

There must be another way to assign the same js function to any and all of the assigned buttons. 必须有另一种方式来将相同的js函数分配给所有分配的按钮。

The problem I'm having is that if I have only 1 function; 我遇到的问题是,如果我只有1个功能; whenever I click on the first image, the menu pops up which is great. 每当我单击第一个图像时,都会弹出菜单,这很棒。 But then when I click on the following or any other image's button, it only opens up the menu on the first image again, and does not open the menu on the selected image. 但是,当我单击以下图像或任何其他图像的按钮时,它只会再次打开第一张图像上的菜单,而不会打开所选图像上的菜单。

So for example I want to be able to have: <button onclick="function()"></button> , and that should be able open a menu on any of the image's buttons. 因此,例如,我希望能够拥有: <button onclick="function()"></button> ,并且应该能够在图像的任何按钮上打开菜单。

There must be an easier solution rather than writing out 200 js functions. 必须有一个简单的解决方案,而不是写出200个js函数。

Thanks in advance. 提前致谢。

My PHP code: 我的PHP代码:

<button onclick="toggle_pinit()" type="button" id="pinit_button"><img src="1.png"/></button>

and my Javascript code: 和我的Javascript代码:

function toggle_pinit() 
{
    var button = document.getElementById('pinit_button');
    {
        var div = document.getElementById('pinit_menu'); // display select_show
        if (div.style.visibility == 'hidden') 
        {
            div.style.visibility = 'visible';
        }
        else 
        {
            div.style.visibility = 'hidden';
        }
    }
}

First of all, IDs should be unique. 首先,ID应该是唯一的。 If multiple elements should have it, use a class instead. 如果应该有多个元素,请改用一个类。 Then, using JS directly in your HTML is not really a good practice. 然后,直接在HTML中使用JS并不是一个好习惯。 Separate your HTML from your styles and your scripts. 将HTML与样式和脚本分开。

That beying said, here is how I would do it: 那样说,这就是我要怎么做:

 // Wait until the document is loaded window.addEventListener('DOMContentLoaded', function() { // Find all pin buttons and put them into an iterable Array var pinBtns = Array.from(document.querySelectorAll('.pinit_button')), // Find the menu pinMenu = document.getElementById('pinit_menu'), // Just for the demo pinSrc = document.getElementById('src'), selectedSrc = ''; // For each of the buttons pinBtns.forEach(function(btn) { // Listen for clicks on them btn.addEventListener('click', togglePin); }); // Do whatever you want here function togglePin(e) { // Get the src of the clicked image var newSrc = e.target.src; // If it was already selected, close the menu if (selectedSrc === newSrc) { pinMenu.classList.remove('open'); // Reset the image src selectedSrc = ''; // Otherwise, open it } else { pinMenu.classList.add('open'); // Show the src in the menu's text pinSrc.innerText = e.target.src; // Store it for later use selectedSrc = newSrc; } } }); 
 body { font-size: 12px; font-family: Arial, Helvetica, sans-serif; } .pinit_button { cursor: pointer; } #pinit_menu { margin: 1em 0; padding: .5em; background: #0072ff; color: #fff; display: none; } #pinit_menu.open { display: block; } 
 <p>Click on an image:</p> <button class="pinit_button"><img src="https://placeimg.com/50/50/animals"></button> <button class="pinit_button"><img src="https://placeimg.com/50/50/arch"></button> <button class="pinit_button"><img src="https://placeimg.com/50/50/nature"></button> <button class="pinit_button"><img src="https://placeimg.com/50/50/people"></button> <button class="pinit_button"><img src="https://placeimg.com/50/50/tech"></button> <div id="pinit_menu">Here you can do whatever you need, knowing that the image you clicked on has this src: <span id="src"></span></div> 

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

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