简体   繁体   English

使用单独的onclick事件修改onclick

[英]Modify onclick with a separate onclick event

I have three clickable images. 我有三个可点击的图像。 One image on the first row, two images on the second row. 第一行有一个图像,第二行有两个图像。 The image on the first row is a reflection of whichever image on the second row was last clicked. 第一行上的图像反映了最后单击第二行上的图像。 I can make the image on the second row modify the src of the image on the first row, but am having difficulty modifying the onclick parameter of the anchor tag. 我可以使第二行上的图像修改第一行上图像的src,但是很难修改锚标记的onclick参数。 I can't just change the href of the first image, it has to actually mimic the clicking of the second row image. 我不能只更改第一张图片的href,它实际上必须模仿第二行图片的点击。 Here's my code, along with by a jsfiddle of it: 这是我的代码,以及其中的jsfiddle:

https://jsfiddle.net/2hgzmnmb/ https://jsfiddle.net/2hgzmnmb/

<a onclick="document.getElementById('image1').click();" id="previewLink"><img id="previewImage" src="http://i.imgur.com/UevhwuA.png"></a>

<br><br> 

<a id="image1" onclick="document.getElementById('previewImage').src='http://i.imgur.com/UevhwuA.png';alert('1')"><img src="http://i.imgur.com/UevhwuA.png"></a>

<a id="image2" onclick="document.getElementById('previewImage').src='http://i.imgur.com/121fy0E.png';alert('2')"><img src="http://i.imgur.com/121fy0E.png"></a>

I want to add the following: document.getElementById('previewLink').onclick='document.getElementById('image1').click();' 我想添加以下内容: document.getElementById('previewLink').onclick='document.getElementById('image1').click();' to the onclick parameter for the first image on the bottom row, and: document.getElementById('previewLink').onclick='document.getElementById('image2').click();' 设置为底行第一张图片的onclick参数,以及: document.getElementById('previewLink').onclick='document.getElementById('image2').click();' to the second image on the bottom row, so the image on the first row will not only look like whichever image was last clicked on the second row, but will also mimic the actual click. 移至底行的第二张图片,因此第一行的图片不仅看起来像最后点击第二行的那张图片,而且还会模仿实际的点击情况。 Right now, obviously from the code, it will only mimic clicking the first image on the second row, no matter what image was last clicked, and I'm thinking it's because my syntax isnt correct when I try to modify the onclick property of the anchor tag for the image on the first row. 现在,很明显,从代码中来看,无论最后单击的是哪个图像,它都只会模仿单击第二行中的第一张图像,并且我想这是因为当我尝试修改该行的onclick属性时我的语法不正确第一行上图像的定位标记。

I also tried doing this with variables but got a bit tied in knots. 我也尝试使用变量来执行此操作,但有点麻烦。 Any ideas on how I could go about doing this? 关于如何执行此操作的任何想法? I'm close, just not there yet. 我很近,只是还没到那儿。

Thanks! 谢谢!

I'd remove the inline handlers and use addEventListener() instead - except for demo purposes I've left your alert() messages in the inline onclick attributes to simulate the click behaviour that you want clicks on the preview image to mimic. 我将删除内联处理程序,而改用addEventListener() -出于演示目的,我将您的alert()消息留在了内联onclick属性中,以模拟希望模拟预览图像的单击行为。

By putting the logic into JS in a <script> element you'll find it much easier to maintain because you can format the code in a readable manner. 通过将逻辑放入<script>元素中的JS中,您会发现它易于维护,因为您可以以可读的方式格式化代码。

Then simply use a variable to keep track of what the currently selected image is: 然后只需使用一个变量来跟踪当前选择的图像是什么:

 // the following JS should be in a script element at the end of the body // and/or wrapped in a window onload handler function imageClick(e) { currentImage = e.currentTarget; document.getElementById("previewImage").src = currentImage.querySelector("img").src; } var images = document.querySelectorAll(".image"); for (var i = 0; i < images.length; i++) { images[i].addEventListener("click", imageClick); } var currentImage = images[0]; // default to first image document.getElementById('previewLink').addEventListener("click", function() { currentImage.click(); }); 
 <a id="previewLink"><img id="previewImage" src="http://i.imgur.com/UevhwuA.png"></a> <br><br> <a id="image1" class="image" onclick="alert('1')"><img src="http://i.imgur.com/UevhwuA.png"></a> <a id="image2" class="image" onclick="alert('2')"><img src="http://i.imgur.com/121fy0E.png"></a> 

Note also that I added a class to your lower images in order to make it easy to process them in a loop to add their click handler. 还要注意,我为您的下部图像添加了一个类,以使其易于循环处理以添加其单击处理程序。 This is optional, you could select by id instead with .querySelectorAll("#image1,#image2") . 这是可选的,您可以使用id代替.querySelectorAll("#image1,#image2")

add the line below to each anchor tag in the second row 将下面的行添加到第二行中的每个锚标记

document.getElementById('previewLink').related_id=this.id

 <a onclick="if(document.getElementById(this.related_id)!=null){document.getElementById(this.related_id).click()};" id="previewLink"><img id="previewImage" src="http://i.imgur.com/UevhwuA.png"></a> <br><br> <a id="image1" onclick="this.related_id=this.id;document.getElementById('previewImage').src='http://i.imgur.com/UevhwuA.png';alert('1');document.getElementById('previewLink').related_id=this.id"><img src="http://i.imgur.com/UevhwuA.png"></a> <a id="image2" onclick="this.related_id=this.id;document.getElementById('previewImage').src='http://i.imgur.com/121fy0E.png';document.getElementById('previewLink').related_id=this.id;alert('2')"><img src="http://i.imgur.com/121fy0E.png"></a> 

Fiddle: https://jsfiddle.net/2hgzmnmb/1/ 小提琴: https : //jsfiddle.net/2hgzmnmb/1/

this: document.getElementById('previewLink').onclick='document.getElementById('image1').click();' 这: document.getElementById('previewLink').onclick='document.getElementById('image1').click();' should be: document.getElementById('previewLink').onclick=this.onclick 应该是: document.getElementById('previewLink').onclick=this.onclick

because onclick attribute is actually running a JS code, so if you pass a string there, it will interpret it as just a string. 因为onclick属性实际上正在运行JS代码,所以如果在其中传递字符串,它将把它解释为一个字符串。

onclick='document.getElementById('image1').click();' is the same as the string "document.getElementById('image1').click();" 与字符串"document.getElementById('image1').click();" , it does nothing. ,它什么也不做。

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

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