简体   繁体   English

如何仅将jQuery中当前悬停的元素作为目标

[英]How to target only the current hovered element in jQuery

I'm trying to figure out how to target an element when hovered but the thing is it shares same class with other element on the page. 我试图弄清楚如何在悬停时定位一个元素,但问题是它与页面上的其他元素共享同一类。

Here is what i'm doing... i'm working on a wooccomerce page in which i setting the entry_title class to nowrap , so that long titles can be on one line but i want to do it such a way that when user hover the product with long title, the full title appears (ie add a class to the element and set white-space: normal ) 这是我正在做的...我正在处理wooccomerce页面,在该页面中,我将entry_title类设置为nowrap ,以便长标题可以在一行上,但是我想要这样一种方式:当用户悬停时带有长标题的产品,将显示完整标题(即,将一个类添加到元素并设置white-space: normal

Below is the CSS i'm using to make the title one line with ellipses 以下是我用来使标题用省略号一行的CSS

.woocommerce .product h4.entry-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: .3s;
}
.show_ftitle {
    white-space: normal !important;
}

Then the jQuery is as follow 然后jQuery如下

jQuery(document).ready(function($) {
    $('.wf-cell').hover(function() {
        $('.entry-title').addClass('show_ftitle');
    }, function() {
        $('.entry-title').removeClass('show_ftitle');
    });
});

Now the issue is that if i hover any of the product... the title of long product (in which i didn't hover) will show. 现在的问题是,如果我悬停任何产品,则会显示长产品的标题(我没有悬停在其中)。 only want it to work for the current hovered element. 只希望它适用于当前悬停的元素。 but i only have class to work with. 但我只有class可以合作。 Is there a way i can pin-point this and make it work only for the product that my cursor is hovered on. 有没有办法我可以指出这一点,并使它仅适用于将光标悬停在其上的产品。

Thanks 谢谢

You can simply add this with a comma after the class. 您可以简单地添加this与课后一个逗号。 Example using your code below: 使用以下代码的示例:

jQuery(document).ready(function($) {
$('.wf-cell').hover(function() {
    $('.entry-title', this).addClass('show_ftitle');
}, function() {
    $('.entry-title', this).removeClass('show_ftitle');
});

}); });

Hope it helps. 希望能帮助到你。

There is no need for JavaScript, just do it with css hover 无需JavaScript,只需将CSS悬停即可

.wf-cell:hover .entry-title {
  white-space: normal !important;
}

Problem with your code is you are selecting all the elements, not the one you are using 代码的问题是您正在选择所有元素,而不是正在使用的元素

$('.wf-cell').hover(function() {
  $(this).find('.entry-title').addClass('show_ftitle');
}, function() {
  $(this).find('.entry-title').addClass('show_ftitle');
});

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

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