简体   繁体   English

如何在多个类jQuery中使用'this'

[英]How to use 'this' in multiple classes jquery

I am hiding 'hidden-items' class li elements using jquery and want to show them once more tag link is clicked. 我正在使用jquery隐藏“隐藏项目”类li元素,并希望在单击更多标签链接后显示它们。 The jquery part is working, but all the list items are being showed. jQuery部分正在运行,但是所有列表项都已显示。 I searched about this and found out about 'this' selector. 我搜索了一下,然后发现了“ this”选择器。 But I am confused on how to use this to show items that are close to more tags link. 但是我对如何使用它来显示接近更多标签链接的项目感到困惑。

<div class="tag-box">
 <ul class='gk-tags'>
  <li><a href='/category/economic' class='gk-tag'>Economic</a></li>
  <li><a href='/category/test' class='gk-tag'>Test</a></li>
  <li><a href='/category/sports' class='hidden-items gk-tag'>Sports</a></li>
  <li><a href='/category/health' class='hidden-items gk-tag'>Health</a></li>
 </ul>
 <a class='tag-show-more'>more tags</a>
</div>

<div class="tag-box">
 <ul class='gk-tags'>
  <li><a href='/category/test' class='gk-tag'>Test</a></li>
  <li><a href='/category/sports' class='hidden-items gk-tag'>Sports</a></li>
  <li><a href='/category/health' class='hidden-items gk-tag'>Health</a></li>
 </ul>
 <a class='tag-show-more'>more tags</a>
</div>

<script type="text/javascript">
jQuery(document).ready(function(){
  jQuery('.hidden-items').hide();
  jQuery('.tag-show-more').click(function(){
     jQuery('.hidden-items').show();
  });
});
</script>

Based on the structure of your HTML, you can use .prev() and .find() with this like: 基于HTML的结构,你可以使用.prev().find()this类似:

jQuery(this).prev('.gk-tags').find('.hidden-items').show();

 jQuery(document).ready(function(){ jQuery('.hidden-items').hide(); jQuery('.tag-show-more').click(function(){ jQuery(this).prev('.gk-tags').find('.hidden-items').show(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="tag-box"> <ul class='gk-tags'> <li><a href='/category/economic' class='gk-tag'>Economic</a></li> <li><a href='/category/test' class='gk-tag'>Test</a></li> <li><a href='/category/sports' class='hidden-items gk-tag'>Sports</a></li> <li><a href='/category/health' class='hidden-items gk-tag'>Health</a></li> </ul> <a class='tag-show-more'>more tags</a> </div> <div class="tag-box"> <ul class='gk-tags'> <li><a href='/category/test' class='gk-tag'>Test</a></li> <li><a href='/category/sports' class='hidden-items gk-tag'>Sports</a></li> <li><a href='/category/health' class='hidden-items gk-tag'>Health</a></li> </ul> <a class='tag-show-more'>more tags</a> </div> 

just use $(this) for the selection of the current clicked element. 只需使用$(this)来选择当前单击的元素。

then use .parents('.classNameOfParent') to get the parent element 然后使用.parents('.classNameOfParent')获取父元素

then hide the parent element 然后隐藏父元素

.hide()

you could do this like that: 您可以这样做:

 $(this).parents('.classNameOfParent').hide()

good luck! 祝好运!

 jQuery(document).ready(function(){
      jQuery('.hidden-items').hide();
      jQuery('.tag-show-more').click(function(){
         jQuery(this).closest(".tag-box").find('.hidden-items').show();
      });
    });

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

你可以这样

jQuery(this).siblings('.gk-tags').find('.hidden-items').show();

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

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