简体   繁体   English

onmouseover / onmouseout与chrome上的JQuery .hover发生冲突?

[英]onmouseover / onmouseout conflicting with JQuery .hover on chrome?

This is working fine on Firefox but not on Chrome or Safari. 这在Firefox上工作正常,但在Chrome或Safari上却无法正常工作。 I had jQuery .hover and was working fine but when I added a inline onmouseover / onmouseout to a image inside that div, the .hover (.main-description) wont show. 我使用jQuery .hover并运行良好,但是当我向该div中的图像添加内联onmouseover / onmouseout时,.hover(.main-description)将不会显示。 It actually changes the state to "block" dynamically when I hover but the text block (.main-description) wont show. 当我悬停时,它实际上将状态动态更改为“阻止”,但是文本阻止(.main-description)不会显示。

HTML: HTML:

<img src="no-hover.png" 
width="250" height="250" title="me"
onmouseover="this.src='hover.png';" 
onmouseout="this.src=no-hover.png';">

JS: JS:

$(".feature-main").hover(
    function() {
        $(this).children(".main-description").show();
    }, 
    function() {
        $(this).children(".main-description").hide();
    }

Any help would be appreciated. 任何帮助,将不胜感激。 Should I come with a different solution perhaps? 我是否应该使用其他解决方案? Move the onmouseover / onmouseout to JS too? 也将onmouseover / onmouseout移到JS吗?

You can check out the site at here 您可以在这里查看网站

Thank you! 谢谢!

Hi @Marcio it is not a good practice to make two functionalities for the same thing. 嗨,@ Marcio,对同一件事做两个功能不是一个好习惯。 You need to move the src replacement in the jquery code. 您需要在jQuery代码中移动src替换。 I suggest something like this: 我建议是这样的:

$(".feature-main").hover(
function() {
    $(this).children(".main-description").show();
    $(this).attr('src', 'hover.png');
}, 
function() {
    $(this).attr('src', 'no-hover.png');
    $(this).children(".main-description").hide();
}

What you have show in the url i do not see a problem on the image, but your approach is not good for the separate logic. 您在url中显示的内容我在图像上看不到问题,但是您的方法不适用于单独的逻辑。

Instead go unobtrusive with mouseenter/mouseleave and remove the inline event handlers: 相反,不要对mouseenter/mouseleavemouseenter/mouseleave并删除内联事件处理程序:

$(".feature-main").on({
    mouseenter:function() {
        $(this).attr('src', 'hover.png')
               .find(".main-description").show();
    }, 
    mouseleave:function() {
        $(this).attr('src', 'no-hover.png')
               .find(".main-description").hide();
    }
});

use .find() method if your target element is child of another child element. 如果目标元素是另一个子元素的子元素,请使用.find()方法。

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

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