Triggering :hover on second element when hovering on first

拟墨画扇 提交于 2021-01-28 08:03:19

问题


I've got this test page going on: http://joshuacody.net/qworky/

Warning: Tons of CSS3. This will only look right in Safari or Chrome. Firefox will be tolerable. All else will be lost.

Essentially, when I hover on someone's name, I'd like it to trigger the hover state on their image as well. It's working now, but I feel the solution is inelegant. I'd rather not add more classes. Here it is:

$('a.name_link').hover(function(){
    $(this).parent('p').siblings('img').toggleClass('link_hover');
});

$('img.name_img').hover(function(){
    $(this).siblings('p').children('a').toggleClass('hover');
});

I thought the following would work:

$('a.name_link').hover(function(){
    $(this).parent('p').siblings('img').trigger('hover');
});

Any idea why it's not? I'm really looking for the cleanest, simplest way to do this. Thanks so much!


回答1:


from what i understand of the jquery hover method is that it accepts two functions

    $('a.name_link').hover(
               //mouse over
               function(){
                 $(this).parent('p').sibling('img').toggleClass('link_hover');
                },
               // mouse out
               function(){

               }
        );

since you're only declaring the first method, then maybe you should try triggering the mouseover method

$('a.name_link').hover(function(){
     $(this).parent('p').siblings('img').trigger('mouseover');
});




回答2:


Instead of just one function, the hover function is supposed to have 2 functions. Try putting both functions on there.




回答3:


Turns out I could just rework my CSS to do that, putting the img inside of the anchor tag.



来源:https://stackoverflow.com/questions/2099380/triggering-hover-on-second-element-when-hovering-on-first

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!