简体   繁体   English

使用jQuery只定位同一类中的一个元素

[英]Target only one element within same classes with jQuery

I have a little issue: 我有一个小问题:

(function ($) {
    $(document).ready(function() {
        $(".suaugusi").click(function () {
            $(".activ-suaug, .activ-title-suaugusi").slideToggle("slow");
        });
    });
})(jQuery);

How to use code above, to make sure, taht it works only on one DIV with class .suaugusi? 确保使用上面的代码,仅适用于具有.suaugusi类的一个DIV I have 12 div's with same classes, but i need, that it would activate only on clicked one. 我有12个具有相同类的div,但是我需要,它只能在单击的那个上激活。 How to do so? 怎么做?

EDIT 编辑

<div class="col-md-4 col-xs-12 uzsiemimas">
  <div class="portfolio-item suaugusi">
    <div class="item-inner">
      <img class="img-responsive" src="<?php the_sub_field('nuotrauka'); ?>" alt="">
      <h4 class="activ-title-suaugusi"><?php the_sub_field('pavadinimas'); ?></h4>
      <div class="overlay"></div>
      <div class="activ-menu activ-suaug">
        <?php if( have_rows( 'uzsiemimo_veiklos') ): while ( have_rows( 'uzsiemimo_veiklos') ) : the_row(); ?>
        <a href="<?php the_sub_field('nuoroda'); ?>">
          <div class="activ-element">
            <?php the_sub_field( 'pavadinimas');?>
          </div>
        </a>
        <?php endwhile; else : endif; ?>
      </div>
    </div>
  </div>
</div>

You can use the this keyword within the event handler to refer to the element which raised the event, then find() to retrieve the specific elements you want to target. 您可以在事件处理程序中使用this关键字引用引发事件的元素,然后使用find()检索要定位的特定元素。 Try this: 尝试这个:

(function ($) {
    $(document).ready(function() {
        $(".suaugusi").click(function () {
            $(this).find('.activ-suaug, .activ-title-suaugusi').slideToggle("slow");
        });
    });
})(jQuery);

As activ-suaug and activ-title-suaugusi are child elements of suaugusi you should use .find() to traverse up to them, then perform the desired operation. 由于activ-suaugactiv-title-suaugusiactiv-title-suaugusi的子元素, suaugusi应使用.find()遍历它们,然后执行所需的操作。

Use 采用

$(".suaugusi").click(function () {
    $(this).find(".activ-suaug, .activ-title-suaugusi").slideToggle("slow");
});

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

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