简体   繁体   English

无法使用jquery ui持续时间参数或CSS使悬停事件对目标元素的同级对象起作用

[英]can't get a hover event to work on a sibling of a target element using jquery ui duration paramater or CSS

My goal is to change the background color of an element and one of its siblings that is higher in the DOM but in the same parent on hover. 我的目标是更改元素及其兄弟姐妹之一的背景颜色,该颜色在DOM中较高,但在悬停时位于同一父对象中。 I was able to use css transition to change the first element but i couldn't get the sibling to change. 我能够使用CSS转换来更改第一个元素,但我无法获得更改的兄弟姐妹。 so I looked into jquery UI addClass effect 所以我研究了jQuery UI的addClass效果

I wanted the code below to work since I couldn't get a css solution to work, the goal was to change both elements on hover 我希望下面的代码能够正常工作,因为我无法使用CSS解决方案,目标是在悬停时更改两个元素

$(document).ready(function(){
        $('.circletag').hover(function() {
             $(this).addClass( "red");
             $(this).parent().find('title').addClass('red', 2000);
        }, function() {
            $(this).removeClass('red', 5000);
            $(this).parent().find('title').removeClass('red', 2000);
        });
})

I was able to get a fade effect on the $(this) elements but the .title element is not showing any changes at all. 我可以在$(this)元素上获得淡入淡出的效果,但是.title元素根本没有显示任何更改。

when .circletag is hovered I would like .circletag backgroud to change and the .title background to change at the same time over a 2 second interval. .circletag悬停我想.circletag化背景改变和.title背景在同一时间在2秒的时间间隔发生变化。 If It cant be done with css which is the way I would prefer the solution I would appreciate a jquery one. 如果它不能用css完成,那是我更喜欢的解决方案,那我将感谢一个jquery。

Also I'm curios to know why the console says 我也很想知道为什么控制台说

SyntaxError: syntax error

    .ui-helper-hidden {

Why does the duration not work in this addClass function when im using jquery ui? 当我使用jquery ui时,为什么持续时间在此addClass函数中不起作用? So weird for me. 对我来说很奇怪。 why is it that when the mouse is moves off the element it does not take 5 seconds to remove the class? 为什么当鼠标从元素上移开时,不需要5秒钟即可删除该类? it looks like the css transition rule is calling the shots. 看起来CSS过渡规则正在做主。

basically I want the div with the class of .title 's backgound and .circletag background to fade in and out on hover of .circletag . 基本上我想用类的div .title的backgound和.circletag背景淡入淡出上悬停.circletag jsfiddle 的jsfiddle

Thanks for your help guys. 感谢您的帮助。

Try it like this: 像这样尝试:

$(document).ready(function(event){
    $('.circletag').hover(function() {
         $(this).addClass( "red");
         $(this).parent().find('.title').addClass('red', 2000);
    }, function() {
        $(this).removeClass('red', 5000);
        $(this).parent().find('.title').removeClass('red', 2000);
    });
 })

You need to specify the class selector in 'find' function.. Let me know :) 您需要在“查找”功能中指定类选择器。让我知道:)

Try this, 尝试这个,

$(this).parent().find('.title').addClass('red', 2000);

you need to use ".title" inside your find() if you are referring a class. 如果您要引用一个类,则需要在find()中使用“ .title”。

Fiddle 小提琴

Hope this helps. 希望这可以帮助。

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

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