简体   繁体   English

如何获取一张图片以更改为我使用javascript单击的图片

[英]How to get one image to change to the image i click on using javascript

Hi I'm trying to create a gallery using JavaScript. 嗨,我正在尝试使用JavaScript创建画廊。 I want to have a row of small images at the bottom and when you click on one it will display that image in a bigger window above. 我想在底部有一排小图像,当您单击一个图像时,它将在上方的较大窗口中显示该图像。 and that window currently contains and image. 该窗口当前包含和图片。

New to JavaScript so not 100% sure what I'm doing. JavaScript的新手,所以不能100%知道我在做什么。 here is what I was trying to use but wasn't working. 这是我试图使用但无法正常工作的东西。 Any tips would be great thanks.: 任何提示将非常感谢。:

<script>
function changeimage(val)
{
    var x = val.getAttribute("src");
    var y = getElementById("Display");
    var z = y.getAttribute("src");
    z.setAttribute("src","x");
}
</script>


<img class="mid_one" src="Images/home3.jpg" alt="home1" width="186px" height="186px" onclick="changeimage(this)"/>

Thanks in advance. 提前致谢。

<img name="Display" id="Display" src="Images/home2.jpg" width="450" height="450" alt="Displayed Image"/>

edit: added display 编辑:添加显示

Try this 尝试这个

function changeimage(val)
{
    var x = val.getAttribute("src");
    var y = getElementById("Display");
    y.setAttribute("src",x);
}

According to your logic var z = y.getAttribute("src"); 根据您的逻辑var z = y.getAttribute("src"); is a string and not an Element .. In the next line you seem to set the attribute to the string .. 是一个字符串而not an Element ..在下一行中,您似乎将属性设置为字符串..

You should set the attribute on the element ( y ), you can't set an attribute on an attribute value ( z ). 您应该在元素( y )上设置属性,而不能在属性值( z )上设置属性。

Also, you should use the variable x to get the source from the other image, not the string "x" . 另外,您应该使用变量x从其他图像获取源,而不是字符串"x"

var x = val.getAttribute("src");
var y = document.getElementById("Display");
y.setAttribute("src", x);

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

相关问题 如何使用香草JavaScript更改点击图片 - How to change an image on click using vanilla JavaScript 如何使用JavaScript在每次点击图片时更改图片标记的URL? - How to change the URL of an image tag on every click on the image using JavaScript? 如何使用onclick事件单击一个图像,并在同一网页上更改另一个图像? - How can I click on one image using the onclick event and have it change another image on the same webpage? 如何使用javascript更改图像? - How do I change an image using javascript? 当我使用Javascript单击导航菜单链接时,如何更改页面的背景图像? - How can i change background image of the page when i click on navigation menu link using Javascript? 如何使用JavaScript更改点击时的图片来源? - How can I change the image source on click with JavaScript? Javascript 点击更改图片 - Javascript Change image on click 当我单击链接时如何使用javascript在CSS中更改图像中的选定颜色 - when i click link how to change selected color in image in css using javascript 如何通过单击Javascript中的图像来更改内容? - How to change the content with click of an image in Javascript? 图像单击我给了一个功能,单击它获取图像ID,使用它我必须使用javascript更改图像 - to image click I gave a function, on clicking it am getting image id, using this I have to change the image using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM