简体   繁体   English

如何将图像 ID 作为 function 参数发送?

[英]How to send image id as function parameter?

So i have this function:所以我有这个 function:

function toggle()
{
  var Blur=document.getElementById('blur');
  Blur.classList.toggle('active');
  var PopUp=document.getElementById('PopUp');
  PopUp.classList.toggle('active');
}

which when you click an image opens a preview for that image.当您单击图像时,会打开该图像的预览。 Instead of creating n functions for n photos on my website is there any way to send an image id as parameter?除了在我的网站上为 n 张照片创建 n 个函数之外,还有什么方法可以将图像 id 作为参数发送? I'm thinking something like:我在想类似的事情:

function toggle(IdParameter)
{
  var Blur=document.getElementById('blur');
  Blur.classList.toggle('active');
  var PopUp=document.getElementById('IdParameter');
  PopUp.classList.toggle('active');
}

You can use "this" and paste image as element to the function.您可以使用“this”并将图像作为元素粘贴到 function。 Then you can get what you need from the image: src, alt, id, class..然后就可以从图片中得到你需要的东西了:src, alt, id, class..

 function myFunction(e){ console.log(e.src) }
 <img src="img_1.jpg" alt="image1" onclick="myFunction(this);"> <img src="img_2.jpg" alt="image2" onclick="myFunction(this);">

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

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