簡體   English   中英

如何將容器定位到元素頂部?

[英]How to target a container on the top of an element?

<div class="a"></div> //notice the the container is closed
<div class="b"></div>

我知道您可以使用“ .a + div”類來定位.b類,但是我該如何做呢?

我想在單擊.b類時隱藏.a類。

嘗試下面的jQuery代碼

$('.b').click(function(){
 $(this).prev('div').hide();
});

使用hide()方法隱藏div。 因此,您可以在.b的click事件上隱藏div。

 $('.b').on('click',function(){ $(this).prev('.a').hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="a">a</div> //notice the the container is closed <div class="b">div b</div> 

這將起作用並且也是直接的方法

 $(function(){
      $('.b').click(function(){
        $('.a').hide();
      });
   });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM