JavaScript鼠标悬停图像
我试图用鼠标来改变图片,但它似乎不起作用?有人能帮忙吗?这是我的密码JavaScript鼠标悬停图像,javascript,html,mouseover,Javascript,Html,Mouseover,我试图用鼠标来改变图片,但它似乎不起作用?有人能帮忙吗?这是我的密码 <img id="myImg" src="Nov1.jpg" alt="November" style="width:452px;height:127px"> <script> $('#myImg').hover(function() { $(this).attr('src', '/folder/nov2014.jpg'); }, function() { $(this).
<img id="myImg" src="Nov1.jpg" alt="November" style="width:452px;height:127px">
<script>
$('#myImg').hover(function() {
$(this).attr('src', '/folder/nov2014.jpg');
}, function() {
$(this).attr('src', '/folder/Nov1.jpg');
});
</script>
尝试:
可能有两个原因会造成这样的问题 在使用jQuery库之前,请查看是否包含了它。 您必须将代码放在$document中; 这样试试看 HTML: 为什么你的代码不起作用 在操作任何DOM元素之前,必须首先加载页面。因此产生了一个问题 我怎么知道页面是否已经加载 下面是$document.ready;作为jQuery的帮助之手。当DOM元素准备好使用时,它允许javaScript执行它包含的代码。因此,作为开发人员,您不必担心DOM是否已加载 这就是为什么要在DOM元素myImg可用之前对其进行操作的原因。并且您的javaScript/jQuery代码找不到您所要求的
第一个位置中的“/”表示系统的根。使用相对的方法。另外,尝试使用非悬停,而是mouseover和mouseoutAlso,当处理mouseout时,将image的src改回“/folder/Nov1.jpg”,而不是改回Nov1.jpg
<img id="myImg" src="Nov1.jpg" alt="November" style="width:452px;height:127px">
<script>
$("#myImg")
.mouseover(function() {
$(this).attr("src", "/folder/nov2014.jpg");
})
.mouseout(function() {
$(this).attr("src", "/folder/Nov1.jpg");
});
</script>
<img id="myImg" src="http://www.allgraphics123.com/ag/01/14142/14142.gif" />
$(document).ready(function(){
$('#myImg').hover(function() {
$(this).attr('src', 'http://thumb7.shutterstock.com/display_pic_with_logo/265489/120305665/stock-vector-cartoon-parrot-vector-clip-art-illustration-with-simple-gradients-all-in-a-single-layer-120305665.jpg');
}, function() {
$(this).attr('src', 'http://www.allgraphics123.com/ag/01/14142/14142.gif');
});
});