How to get the children of the $(this) selector?

前端 未结 18 2206
日久生厌
日久生厌 2020-11-22 05:10

I have a layout similar to this:

and would like to use a jQuery selector to selec

18条回答
  •  醉话见心
    2020-11-22 05:43

    Here's a functional code, you can run it (it's a simple demonstration).

    When you click the DIV you get the image from some different methods, in this situation "this" is the DIV.

    $(document).ready(function() {
      // When you click the DIV, you take it with "this"
      $('#my_div').click(function() {
        console.info('Initializing the tests..');
        console.log('Method #1: '+$(this).children('img'));
        console.log('Method #2: '+$(this).find('img'));
        // Here, i'm selecting the first ocorrence of 
        console.log('Method #3: '+$(this).find('img:eq(0)'));
      });
    });
    .the_div{
      background-color: yellow;
      width: 100%;
      height: 200px;
    }
    
    
    

    Hope it helps!

提交回复
热议问题