Onmouseout事件无效

时间:2015-01-12 11:32:14

标签: javascript jquery html css

我有这个HTML:

<div class="baloon_signup"  onmouseover="pinkImg(this)" onmouseout="whiteImg(this)">
    <img src="images/prijavi.png" class="imageResponsive" />
</div>

和这个javascript函数:

<script>
function pinkImg(x) 
    {
    x.innerHTML = '<img src="images/prijaviH.png" class="imageResponsive" />';
    }

function whiteImg(x) 
    {
    x.innerHTML = '<img src="images/prijavi.png" class="imageResponsive" />';
    }
</script>

在onmouseover事件上触发的函数pinkImg工作正常,但在onmouseout事件上触发的whiteImg根本不起作用。我检查了图像路径,它们是正确的。当我用鼠标悬停在div上时,图像会发生变化,但是当我出去时鼠标图像保持不变。

提前谢谢

5 个答案:

答案 0 :(得分:0)

尝试使用div id。

<div id="x" class="baloon_signup"  onmouseover="pinkImg(x)" onmouseout="whiteImg(x)">
    <img src="images/prijavi.png" class="imageResponsive" />
</div>

答案 1 :(得分:0)

不要改变div的innerHTML,只需更改图像元素本身的src即可。

也许你的div比导致你的onmouseout事件没有被解雇的图片大。使用示例代码,div的默认宽度为100%。

尝试此示例代码,包括jQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<div class="baloon_signup"  onmouseover="pinkImg()" onmouseout="whiteImg()">
    <img src="http://www.clker.com/cliparts/1/5/4/b/11949892282132520602led_circle_green.svg.thumb.png" class="imageResponsive" />
</div>

<script>
function pinkImg() {
    $('.imageResponsive').attr('src', 'http://www.clker.com/cliparts/5/7/b/5/1194989231691813435led_circle_red.svg.thumb.png');
}

function whiteImg() {
    $('.imageResponsive').attr('src', 'http://www.clker.com/cliparts/1/5/4/b/11949892282132520602led_circle_green.svg.thumb.png');
}
</script>

答案 2 :(得分:0)

这里有jquery的解决方案:http://jsfiddle.net/vgmppkdg/1/和.hover(whatIn,whatOut)函数

HTML:

<div id="baloon_signup">
 <img src="images/prijavi.png" class="imageResponsive" />
</div>

JS:

var $baloon = $('#baloon_signup')

$baloon.hover(function(){
    $baloon.html('x<img src="images/prijaviH.png" class="imageResponsive" />');
}, function(){
    $baloon.html('y<img src="images/prijavi.png" class="imageResponsive" />');
});

答案 3 :(得分:0)

尝试以下JavaScript代码。 (未使用jQuery)

<div class="baloon_signup" onmouseover="pinkImg()" onmouseout="whiteImg()" >
<img id="animate" src="https://www.gstatic.com/android/market_images/web/play_logo_x2.png" class="imageResponsive" />

function pinkImg(){
 //alert('onmouseover');
    document.getElementById("animate").src = "https://www.google.com/images/errors/logo_sm_2.png";
}
function whiteImg(){
 //alert('onmouseout');
    document.getElementById("animate").src = "https://www.gstatic.com/android/market_images/web/play_logo.png";

http://jsfiddle.net/ovkyqqdo/

答案 4 :(得分:0)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

    <title></title>
    <script>
        function pinkImg(x) {
            x.innerHTML = '<img src="http://www.zastavki.com/pictures/originals/2013/Photoshop_Image_of_the_horse_053857_.jpg" class="imageResponsive" style="height:300px;width:300px" />';
            return false;
        }

        function whiteImg(x) {
            x.innerHTML = '<img src="http://images.visitcanberra.com.au/images/canberra_hero_image.jpg" class="imageResponsive" style="height:300px;width:300px" />';
            return false;
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="baloon_signup"  onmouseover="pinkImg(this);" onmouseout="whiteImg(this);">
    <img src="images/prijavi.png" class="imageResponsive" />
</div>

    </form>
</body>
</html>