简体   繁体   English

jQuery .hover()不更改图像

[英]jQuery .hover() not changing image

I've looked around and tried a few things I've seen on here to try to fix this issue but I can't seem to find out why. 我环顾四周,并尝试了一些我在此处看到的方法来尝试解决此问题,但我似乎找不到原因。 I have five images in the toppic class. 我在toppic班上有五张照片。 I want to be able to hover over an image in the toppic class and change the big image (id = Biggin) in my screen with the image that is being hovered over, and then change back to the default when the mouse leaves the image. 我希望能够将鼠标悬停在toppic类中的图像上,并用悬停的图像更改屏幕中的大图像(id = Biggin),然后在鼠标离开图像时将其更改回默认值。 Is there anything blatantly wrong? 有什么公然的错误吗? I copied and pasted from a previous working function and I'm not sure as to what I did wrong this time. 我复制并粘贴了以前的工作功能,但是我不确定这次做错了什么。

$(".toppic").hover(function() {
  var imgsrc = this.src;
  $("#Biggin").attr("src", imgsrc);

})
$(".toppic").mouseout(function() {   
  $("#Biggin").attr("src", ".//Images/IMG_3604.JPG");
})

I had similar issues when starting out using jQuery, and there could be a few reasons why your code isn't working as intended. 在开始使用jQuery时,我遇到了类似的问题,并且可能由于某些原因您的代码无法按预期工作。 The most common reason mine wasn't working is that the jQuery script element was getting activated before the DOM existed. 我不工作的最常见原因是jQuery脚本元素在DOM存在之前就被激活了。

Make sure that the jQuery script element in your html file loads after your DOM loads. 确保在加载DOM之后加载html文件中的jQuery脚本元素。 You can place the jQuery script element at the end of the HTML file so your jQuery would be loaded after the HTML loads. 您可以将jQuery脚本元素放置在HTML文件的末尾,以便在HTML加载后加载jQuery。

I hope that helps. 希望对您有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM