简体   繁体   English

如何通过悬停按钮更改图像? 更多详情如下

[英]how can I change an image by hovering a button? More details below

So basically I want to change an image by passing over the mouse on a button.所以基本上我想通过在按钮上传递鼠标来更改图像。 with the current code I am able to change the image when the button is clicked, but I also need it to change when I put the mouse over the button without having to click it.使用当前代码,我可以在单击按钮时更改图像,但是当我将鼠标放在按钮上而无需单击它时,我也需要更改图像。 Any ideas?有任何想法吗?

<body>
<input type="button" class = "button" onclick="changeImage('https://www.gettyimages.es/gi-resources/images/500px/983794168.jpg')" value="button1" />


<img src="https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80" class = "img" id="firstimage" width="310">



<input type="button" class = "button4" onclick="changeImage('https://images.unsplash.com/photo-1494548162494-384bba4ab999?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8c3VucmlzZXxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80')" value="button2" />

and the javascript:和 javascript:

var image = document.getElementById('firstimage');

function changeImage(uri) {
  image.src = uri;
}

You can try this, just give an Id to your Button, in order to get it after.你可以试试这个,只需给你的按钮一个 ID,以便之后得到它。

var button = document.getElementById("buttonId")
var image = document.getElementById('firstimage')


button.onmouseover = function(uri){
    image.src = uri
}

Do you mean like this?你的意思是这样吗?
like onclick , you can use onmouseover which is same as :hover in cssonclick一样,您可以使用onmouseover ,这与 css 中的:hover相同

 var image = document.getElementById('firstimage'); function changeImage(uri) { image.src = uri; }
 <input type="button" class = "button" onclick="changeImage('https://www.gettyimages.es/gi-resources/images/500px/983794168.jpg')" onmouseover="changeImage('https://www.gettyimages.es/gi-resources/images/500px/983794168.jpg')" value="button1" /> <img src="https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80" class = "img" id="firstimage" width="310"> <input type="button" class = "button4" onclick="changeImage('https://images.unsplash.com/photo-1494548162494-384bba4ab999?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8c3VucmlzZXxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80')" onmouseover="changeImage('https://images.unsplash.com/photo-1494548162494-384bba4ab999?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8c3VucmlzZXxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80')" value="button2" />

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

相关问题 将鼠标悬停在图像上时,如何更改两个或多个图像? - when hovering over an image, how can I make two or more images change? 将鼠标悬停在链接上时如何更改图像? - How can I change the image when hovering over a link? 如何在悬停按钮更改背景图片到另一个? - How to change background image to another upon hovering over button? 将鼠标悬停在矢量的一部分上时,如何放大图像? - How can I enlarge an image when hovering over part of a vector? 悬停div时,如何影响图像的不透明度? - How can I affect Opacity of an image whilst hovering a div? Javascript表单验证,如何启用按钮并检查详细信息(内部更多详细信息) - Javascript form validation, how do I enable a button and check details (more details inside) 在Video.js中,当鼠标悬停在视频上方时,在哪里可以更改大播放按钮的颜色? - In Video.js, where can I change the big play button color when the mouse is hovering over the video? 我如何仅按一次按钮就可以将图像更改3次? - How can i make the image change 3 times with only 1 button press? 如何使用按钮或在一定时间后更改图像? - How can I change image with button or after certain time? 在 HTML 中单击时如何更改按钮的图像 - How can I change an image of a button while clicked on in HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM