2339

私はこれに似たレイアウトを持っています:

<div id="..."><img src="..."></div>

imgjQueryセレクターを使用して、クリック内の子を選択したいと考えてdivいます。

を取得するためにdiv、次のセレクターを用意しました。

$(this)

imgセレクターを使用して子を取得するにはどうすればよいですか?

4

19 に答える 19

2933

jQuery コンストラクターはcontext、選択のコンテキストをオーバーライドするために使用できる という 2 番目のパラメーターを受け入れます。

jQuery("img", this);

これは、次のように使用するのと同じ.find()です:

jQuery(this).find("img");

必要な画像がクリックした要素の直接の子孫のみである場合は、次を使用することもできます.children()

jQuery(this).children("img");
于 2008-11-20T21:27:29.097 に答える
482

使用することもできます

$(this).find('img');

imgの子孫であるすべての を返しますdiv

于 2008-11-20T21:23:15.393 に答える
143

正確に1レベル下がった最初のものを取得する必要がある場合は、次imgのことができます。

$(this).children("img:first")
于 2011-07-21T18:47:31.383 に答える
79

DIV タグの直後に IMG タグが続く場合は、次のものも使用できます。

$(this).next();
于 2011-06-21T13:25:45.817 に答える
74

直系の子は

$('> .child-class', this)
于 2012-07-16T20:07:51.357 に答える
47

img親divのすべての要素は次のように見つけることができます

$(this).find('img') or $(this).children('img')

特定のimg要素が必要な場合は、次のように記述できます

$(this).children('img:nth(n)')  
// where n is the child place in parent list start from 0 onwards

divにはimg要素が1つだけ含まれています。したがって、これについては以下が正しいです

 $(this).find("img").attr("alt")
                  OR
  $(this).children("img").attr("alt")

ただし、divにimg以下のような要素が含まれている場合

<div class="mydiv">
    <img src="test.png" alt="3">
    <img src="test.png" alt="4">
</div>

その場合、上位コードを使用して2番目のimg要素のalt値を見つけることはできません。だからあなたはこれを試すことができます:

 $(this).find("img:last-child").attr("alt")
                   OR
 $(this).children("img:last-child").attr("alt")

この例は、親オブジェクト内で実際のオブジェクトを見つける方法の一般的な考え方を示しています。クラスを使用して、子供のオブジェクトを区別できます。それは簡単で楽しいです。すなわち

<div class="mydiv">
    <img class='first' src="test.png" alt="3">
    <img class='second' src="test.png" alt="4">
</div>

あなたは以下のようにこれを行うことができます:

 $(this).find(".first").attr("alt")

より具体的には:

 $(this).find("img.first").attr("alt")

上記のコードのように、findまたはchildrenを使用できます。詳細については、Childrenhttp ://api.jquery.com/children/およびFindhttp ://api.jquery.com/find/にアクセスしてください。例http://jsfiddle.net/lalitjs/Nx8a6/を参照してください

于 2013-03-22T08:05:27.547 に答える
31

このコードを試してください:

$(this).children()[0]
于 2008-11-20T19:51:32.083 に答える
31

DIV の ID を知らなくても、次のように IMG を選択できると思います。

$("#"+$(this).attr("id")+" img:first")
于 2008-11-20T19:56:20.250 に答える
23

次のいずれかの方法を使用できます。

1 検索 ():

$(this).find('img');

2 人の子供 ():

$(this).children('img');
于 2015-08-14T11:29:57.947 に答える
21

jQueryeachは 1 つのオプションです。

<div id="test">
    <img src="testing.png"/>
    <img src="testing1.png"/>
</div>

$('#test img').each(function(){
    console.log($(this).attr('src'));
});
于 2013-06-26T07:42:16.747 に答える
15

Child Selecorを使用して、親内で使用可能な子要素を参照できます。

$(' > img', this).attr("src");

以下は、への参照がなく、 from 他の関数内で使用できるよう$(this)に参照したい場合です。imgdiv

 $('#divid > img').attr("src");
于 2014-07-25T21:56:40.380 に答える
12

これもうまくいくはずです:

$("#id img")
于 2015-06-21T00:15:29.903 に答える
4

$(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 <IMG>
    console.log('Method #3: '+$(this).find('img:eq(0)'));
  });
});
.the_div{
  background-color: yellow;
  width: 100%;
  height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="my_div" class="the_div">
  <img src="...">
</div>

于 2017-03-05T13:27:33.393 に答える