admin管理员组文章数量:1434913
So I have:
<img src="image1.png"
width="300"
height="115"
onmouseover="this.src='image1.png';"
onmouseout="image2.png">
This is working great, but once I hover and the second image turns on, I need it to turn off when I remove the cursor. Any way to achieve this with the simple inline JS I have?
So I have:
<img src="image1.png"
width="300"
height="115"
onmouseover="this.src='image1.png';"
onmouseout="image2.png">
This is working great, but once I hover and the second image turns on, I need it to turn off when I remove the cursor. Any way to achieve this with the simple inline JS I have?
Share Improve this question edited Jun 29, 2015 at 18:16 ChadF 1,75810 silver badges22 bronze badges asked Jun 29, 2015 at 17:58 user8689user8689 271 silver badge3 bronze badges 2- I think you are looking for mouseOut (Jquery) api.jquery./mouseout Edit : I can't see your example – Core972 Commented Jun 29, 2015 at 17:59
- <code><img src="image1.png" width="300" height="115" onmouseover="this.src='image1.png';" onmouseout="image2.png"></code> – user8689 Commented Jun 29, 2015 at 18:01
2 Answers
Reset to default 3Just undo the change in the same way by setting the src:
<img src="image1.png" width="300" height="115" onmouseover="this.src='image2.png';" onmouseout="this.src='image1.png';">
You could use jQuery and use the .hover function. This function performs both mouseenter
and mouseleave
functions for you so you only need to specify what should happen.
Also, you should specify your styling in a CSS file.
This solution does not supply you with an inline solution, but does reduce your code and separates your concerns.
This is your Javascript. I've used changing text in a div to show you an example rather than toggling an image, because I don't have an image handy, but it should be simple to use this as an example.
$("#hoverExample").hover(
function () {
$(this).text("Mouse has entered");
},
function () {
$(this).text("Mouse has left");
}
);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hoverExample">Hover over this div</div>
版权声明:本文标题:jquery - Javascript onmouseover event working but need to reverse upon mouse "not over" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745624901a2666913.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论