在事件悬停或使用jquery单击时更改当前图像

Change current image on event hover or click with jquery

本文关键字:图像 单击 jquery 悬停 事件      更新时间:2023-09-26

我是jquery的新手。我使用这段代码来更改悬停或单击另一个jquery时的当前图像。但它不起作用!!

<head>
<title> Visite  </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-2.1.1.min.js"> </script>
<script>
$('img').hover(function () {
  $(this).attr('src', 'o.184684.jpg');
})
</script>   
</head>
<body>
<img src="logo11w.png" alt="photo" />
</body>
</html>

我举的这个例子来自http://jsfiddle.net/afzaal_ahmad_zeeshan/8H4MC/

尝试将jQuery代码包装在文档就绪的中

$(document).ready(function() {
    $('img').hover(function () {
        $(this).attr('src', 'o.184684.jpg');
    });
});

或者

$(function() {
    $('img').hover(function () {
        $(this).attr('src', 'o.184684.jpg');
    });
});

这只会让你不知所措。您可能还需要传入鼠标输出功能:悬停

$(function() {
    $('img').hover(function () {
        $(this).attr('src', 'o.184684.jpg');
    }, function() {
        //mouse out function
    });
});