简体   繁体   English

从列表中单击项目时预览图像

[英]Preview image when item is clicked from a list

I'm creating a webpage in html that displays a list of cities on the left of the screen and when a user clicks on a city, an image is loaded on the right of the screen for users to see and when they click on another city that image is replaced with a new image matching the city clicked.我正在 html 中创建一个网页,它在屏幕左侧显示一个城市列表,当用户点击一个城市时,会在屏幕右侧加载一个图像供用户查看,当他们点击另一个城市时该图像将替换为与单击的城市相匹配的新图像。

The aim is for users to see an image of each city they click on目的是让用户看到他们点击的每个城市的图像

这是我的目标的预览

I'm fairly new to this space and would appreciate the help.我是这个领域的新手,非常感谢您的帮助。

you should add each of your images in a specific directory in your project, then add "id" attribute to img tag(" for example #image ")您应该将每个图像添加到项目的特定目录中,然后将“id”属性添加到img标签(“例如#image”)

onclick="showImage(this.id)"

add above line to each of your li tag and an specific id attribute (city name for exmaple).将以上行添加到您的每个li标签和一个特定的 id 属性(例如城市名称)。

then open a script tag above your and paste these line:然后打开你上面的脚本标签并粘贴这些行:

   function showImage(id) {
buildimg = `./images/${id}.jpg`;
document.querySelector('#image').setAttribute('src', buildimg);
}

here I named the images folder "images" in root directory.在这里,我将根目录中的图像文件夹命名为“images”。

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

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