简体   繁体   English

找到与此最接近的类

[英]find the closest class with this

How can I get or find the .bg when I'm clicking on the .button 当我单击.button时如何获取或找到.bg

<div class="button"></div>
<div class="title"></div>

<div class="content">
    <div class="white"></div>
    <div class="bg">
        Content Goes Here
    </div>
</div>

I'm trying to get it with next, but then I don't know how can I get inside of the .content div 我正在尝试下一步,但是后来我不知道如何进入.content div

$(document).ready(function(){
   $('.button').click(function(){
       $(this).next().next() ...
  });
});

I've looked for an answer but all of them was with .closest() which I think I need the opposite of that 我一直在寻找答案,但所有人都使用.closest() ,我认为我需要相反的答案

Try using .siblings and .find() as shown :- 尝试使用.siblings.find() ,如下所示:-

$(document).ready(function(){
   $('.button').click(function(){
      alert($(this).siblings('div.content').find('div.bg').html());
  });
});

Working Demo 工作演示

You need to use .find() after traversing to content div: 遍历到内容div后,需要使用.find()

$('.button').click(function(){
  console.log($(this).next().next().find('.bg'));
});

Working Demo 工作演示

Use this 用这个

$(document).ready(function(){
   $('.button').click(function(){
      console.log($(this).next().next('.content').find('.bg').html()); 
  });
});

Demo here 在这里演示

You can use .nextAll() and .find() for traversing your DOM: 您可以使用.nextAll()和.find()遍历您的DOM:

$('.button').click(function(){
   alert($(this).nextAll('.content').find('.bg').html());
});

Demo 演示

Reference 参考

.nextAll() .nextAll()

.find() 。找()

Actually what I've meant was this: to find only the first one so the others wouldn't be affected 其实我的意思是:只找到第一个,这样其他人就不会受到影响

$(document).ready(function(){
   $('.button').click(function(){
      $(this).siblings('.content:first').find('.bg');
  });
});

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

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