简体   繁体   English

其他内部的jQuery选择器元素

[英]Jquery selector element inside other

I'm having a issue trying to built a nice "products" page, I have the html: 我在尝试构建漂亮的“产品”页面时遇到问题,我使用了html:

    <div class="product-box-2">
     <a href="img/produtos/1-lightbox.jpg" class="lightbox"><img src="img/produtos/1.jpg" alt=""></a>
     <p class="legend">>> Vero mlkshk velit, freegan ullamco commodo put a bird on it cred synth kogi, Vero mlkshk velit, freegan ullamco commodo put a bird on it cred synth kogi.</p>
     <a href="img/produtos/1-lightbox.jpg" class="ampliar lightbox">clique para ampliar</a>
    </div>
    <div class="product-box-2">
     <a href="img/produtos/1-lightbox.jpg" class="lightbox"><img src="img/produtos/1.jpg" alt=""></a>
     <p class="legend">>> Vero mlkshk velit, freegan ullamco commodo put a bird on it cred synth kogi, Vero mlkshk velit, freegan ullamco commodo put a bird on it cred synth kogi.</p>
     <a href="img/produtos/1-lightbox.jpg" class="ampliar lightbox">clique para ampliar</a>
    </div>

And so many times it's needed. 很多次都是需要的。 And then I try to put a nice effect on the .legend, with CSS to seting "display:none" and the Jquery: 然后,我尝试通过CSS将.legend设置为一个不错的效果,使用CSS设置“ display:none”和Jquery:

    $('.product-box-2 a').mouseenter(
    function(){
    $('.legend').fadeIn();
});

$('.product-box-2').mouseleave(
    function(){
    $('.legend').fadeOut();
});

But I have the same classes, and so, all legends appear when I put my mouse over some of the .product-box-2 a... And I have no idea how to select only the .legend inside the .product-box-2 a there I'm in. 但是我有相同的类,因此,当我将鼠标放在某些.product-box-2 a ...上时,所有图例都会出现。而且我不知道如何只选择.product-box中的.legend。 -2我在那儿。

If you want to see this in action, here it is! 如果您想在实际中看到 ,就在这里!

Restrict the scope of the inner selector to the element on which the event occurred by giving it the element as the context. 通过将内部选择器的元素作为上下文,可以将内部选择器的范围限制为发生事件的元素。 See the docs for the signature that accepts a context. 有关接受上下文的签名,请参阅文档

$('.product-box-2 a').mouseenter(
   function(){
    $('.legend',$(this).closest('div')).fadeIn();
});
$('.product-box-2').mouseleave(
    function(){
    $('.legend',this).fadeOut();
});

you can try .children([Selector]) as well 您也可以尝试.children([Selector])

$('.product-box-2 a').mouseenter(
    function(){
    $(this).children('.legend').fadeIn();
});

$('.product-box-2').mouseleave(
    function(){
    $(this).children('.legend').fadeOut();
});

http://api.jquery.com/children/ http://api.jquery.com/children/

What you need is 您需要的是

$(this).find('.legend').fadeIn();

In this case $(this) refers to the element that triggered an event and .find() looks only among its children. 在这种情况下, $(this)表示触发事件的元素, .find()仅在其子元素中查找。

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

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