简体   繁体   English

通过将鼠标悬停在另一个上来触发a:悬停事件

[英]Trigger a :hover event by hovering on an other

I have a jQuery issue; 我有一个jQuery问题; I would like to trigger a :hover event on an element by hovering on an other, the console shows me an error: 我想通过悬停在另一个元素上触发一个:hover事件,控制台显示错误:

Uncaught RangeError: Maximum call stack size exceeded 未捕获RangeError:超出最大调用堆栈大小

Here is my Javascript function : 这是我的Javascript函数:

$(".love").hover(function() { 
    $(".fa-heart").trigger('mouseover'); 
});

Here is my HTML : 这是我的HTML:

                                   <div class="middle-bar grey-dark"></div>
                                        <ol class="container text-center" id="love-inline">
                                            <li>
                                                <h2 class="love inline" id="LOVE-left">Nos coups de</h2>
                                            </li> 
                                            &nbsp; 
                                            <li>
                                                <h2 class="love inline" id="heart"><i class="fa fa-heart hvr-pulse-grow"></i></h2>
                                            </li>
                                            &nbsp;
                                            <li>
                                                <h2 class="love inline" id="LOVE-right">du moment</h2>
                                            </li>
                                        </ol>
                                    <div class="little-middle-bar grey-dark"></div>
                                <div class="end-bar"></div>

Any idea ? 任何的想法 ?

The error you see is because you are triggering a hover in a child element of the one which initially raised the event, causing a recursive loop. 您看到的错误是因为您在最初引发事件的子元素中触发悬停,从而导致递归循环。

A better idea is to use CSS alone to achieve this when either the parent or the child are hovered: 更好的想法是在父母或孩子被盘旋时单独使用CSS来实现这一点:

.love:hover .fa-heart, .fa-heart:hover {
    border: 2px solid #C00;
    /* style as needed ... */ 
}

Also, note that your HTML is invalid; 另请注意,您的HTML无效; only li elements can be direct descendants of ol . 只有li元素可以是ol直接后代。

You cannot trigger the mouseover event in any element. 您无法在任何元素中触发mouseover事件。 And you have been triggering mouseover inside hover function that's why its throwing RangeError . 并且你一直在触发鼠标悬停功能,这就是它抛出RangeError的原因。

To solve your issue, you can do it with simple css rules: 要解决您的问题,您可以使用简单的css规则来完成:

.love:hover .fa-heart:hover {
    color: red;
}

But seeing this still doesn't make sense to me. 但看到这一点对我来说仍然没有意义。 You may just apply: 您可以申请:

.fa-heart:hover {
    color: red;
}

You are facing this due to you are recursively triggering the event. 由于您以递归方式触发事件,因此您将面临此问题。 Due to this infinity hover function calling again and again .. and hence the stack got overflowed. 由于这种无限悬停功能一次又一次地调用..因此堆栈溢出。

This happens because your .fa-heart class was inside the .love class and due to this an hover event called for the parent class. 这是因为你的.fa-heart类在.love类中,并且由于这是一个为父类调用的悬停事件。

solution for your problem is e.stopPropagation(); 你的问题的解决方案是e.stopPropagation(); use this like .. 使用这个..

$('.love').hover(function(e) {
    $('.fa-heart').trigger(e.type);
    e.stopPropagation();

});

As the above code also not worked. 由于上面的代码也没有用。 I worked more on the code and find the correct solution. 我在代码上工作得更多,找到了正确的解决方案。 Please check the below code for the solution. 请检查以下代码以获得解决方案。 If you still facing any problem then let me know. 如果您仍然遇到任何问题,请告诉我。

$('.love').hover(function(e) {
    if(e.target !== this ) {
     return false;   
    } else {
        $('.fa-heart').trigger(e.type);
    }
});

Triggering mouseover event is not the same as real mouse over the element, it will not trigger :hovered state of it. 触发鼠标悬停事件与元素上的真实鼠标不同,它不会触发:hovered状态。

Anyway, you don't need javascript for this, as simple CSS would be enough: 无论如何,你不需要javascript,因为简单的CSS就足够了:

.love:hover .fa-heart {
    color: red;
}

Also make sure HTML structure is valid with li element as direct children of ol . 还要确保HTML结构有效,li元素作为ol直接子元素。

 ol li {list-style-type: none; text-align: center;} .love:hover .fa-heart { color: red; } 
 <link data-require="font-awesome@*" data-semver="4.3.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" /> <ol class="container text-center" id="love-inline"> <li> <h2 class="love inline" id="LOVE-left">Nos coups de</h2> &nbsp; </li> <li> <h2 class="love inline" id="heart"><i class="fa fa-heart hvr-pulse-grow"></i></h2> &nbsp; </li> <li> <h2 class="love inline" id="LOVE-right">du moment</h2> </li> </ol> 

Okay so it was a bit tricky; 好的,这有点棘手; I used Css as many of you said as so : 我使用Css,就像你们许多人所说:

.fa-heart:hover, .love:hover .fa-heart {
// css changes
}

but change my html as so to make it work only when I hover on the text or the Fa-heart (because all your answers made all the div hoverabled) : 但是改变我的html,只有当我将鼠标悬停在文本或Fa-heart上时才能使其工作(因为所有的答案都使得所有的div都可以运行):

<div class="middle-bar grey-dark"></div>
                                            <ol class="container text-center" id="love-inline">
                                                <li class="love inline">
                                                    <h2 class="love inline" id="LOVE-left">Nos coups de</h2>
                                                    &nbsp;<h2 class="love inline" id="heart"><i class="fa fa-heart"></i></h2>&nbsp;
                                                    <h2 class="love inline" id="LOVE-right">du moment</h2>
                                                </li>
                                            </ol>
                                        <div class="little-middle-bar grey-dark"></div>
                                    <div class="end-bar"></div>

Thanks for your help ! 谢谢你的帮助 !

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

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