javascript - 内联切换图像 onclick 事件
问题描述
使用下面的代码片段进行管理,使用内联 onclick javascript 事件从第一张图像更改为第二张图像。我的问题是;当使用另一个内联 Javascript 事件单击第二个图像时,有没有办法将第二个图像更改回第一个图像?IE。无需编写函数。
<img class="first-image" src="/first-image.png" onclick="this.src=\'/second-image.png\'" alt="my images">
解决方案
您可以像这样更新您的代码:
<img class="first-image" src="/first-image.png" onclick="this.src==='/first-image.png' ? this.src='/second-image.png' : this.src='/first-image.png'" alt="my images">
这是一个工作示例:
<img class="first-image" src="https://photo-works.net/images/europe-landscape-photo-edited.jpg" onclick="this.src==='https://photo-works.net/images/europe-landscape-photo-edited.jpg' ? this.src='https://thumbs.dreamstime.com/b/landscape-nature-mountan-alps-rainbow-76824355.jpg' : this.src='https://photo-works.net/images/europe-landscape-photo-edited.jpg'" alt="my images">