私はこれに似たレイアウトを持っています:
<div id="..."><img src="..."></div>
img
jQueryセレクターを使用して、クリック内の子を選択したいと考えてdiv
います。
を取得するためにdiv
、次のセレクターを用意しました。
$(this)
img
セレクターを使用して子を取得するにはどうすればよいですか?
私はこれに似たレイアウトを持っています:
<div id="..."><img src="..."></div>
img
jQueryセレクターを使用して、クリック内の子を選択したいと考えてdiv
います。
を取得するためにdiv
、次のセレクターを用意しました。
$(this)
img
セレクターを使用して子を取得するにはどうすればよいですか?
jQuery コンストラクターはcontext
、選択のコンテキストをオーバーライドするために使用できる という 2 番目のパラメーターを受け入れます。
jQuery("img", this);
これは、次のように使用するのと同じ.find()
です:
jQuery(this).find("img");
必要な画像がクリックした要素の直接の子孫のみである場合は、次を使用することもできます.children()
。
jQuery(this).children("img");
使用することもできます
$(this).find('img');
img
の子孫であるすべての を返しますdiv
正確に1レベル下がった最初のものを取得する必要がある場合は、次img
のことができます。
$(this).children("img:first")
DIV タグの直後に IMG タグが続く場合は、次のものも使用できます。
$(this).next();
直系の子は
$('> .child-class', this)
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/を参照してください
このコードを試してください:
$(this).children()[0]
DIV の ID を知らなくても、次のように IMG を選択できると思います。
$("#"+$(this).attr("id")+" img:first")
次のいずれかの方法を使用できます。
1 検索 ():
$(this).find('img');
2 人の子供 ():
$(this).children('img');
jQueryeach
は 1 つのオプションです。
<div id="test">
<img src="testing.png"/>
<img src="testing1.png"/>
</div>
$('#test img').each(function(){
console.log($(this).attr('src'));
});
Child Selecorを使用して、親内で使用可能な子要素を参照できます。
$(' > img', this).attr("src");
以下は、への参照がなく、 from 他の関数内で使用できるよう$(this)
に参照したい場合です。img
div
$('#divid > img').attr("src");
これもうまくいくはずです:
$("#id img")
$(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>