Yanıtlar:
JQuery yapıcısı context, seçim bağlamını geçersiz kılmak için kullanılabilen adlandırılan 2. parametreyi kabul eder .
jQuery("img", this);
Bu, şu şekilde kullanmakla aynıdır .find():
jQuery(this).find("img");
İstediğiniz imgs yalnızca tıklanan öğenin doğrudan torunlarıysa, şunları da kullanabilirsiniz .children():
jQuery(this).children("img");
Ayrıca kullanabilirsiniz
$(this).find('img');
... ve imgonların torunları olandiv
$(this).children('img')daha iyi gibi görünüyor . Örneğin, <div><img src="..." /><div><img src="..." /></div></div>muhtemelen kullanıcı 1. düzey imgs bulmak istiyor.
İlk almak gerekiyorsa imgaşağı olduğunu tam olarak bir seviye, yapabileceğiniz
$(this).children("img:first")
.children()"tam olarak bir seviye aşağı" :firstnın geldiği ve "ilk" in geldiği yerdir.
.find()yerine kullanırsanız geçerlidir.children()
this, zaten <div>içeren içeren bir referans <img>, ancak sahip olduğunuz referanstan geçmek için birden fazla etiket seviyeniz varsa, kesinlikle kesinlikle birden fazla children()çağrı oluşturabilirsiniz
DIV etiketinizi hemen IMG etiketi izliyorsa, aşağıdakileri de kullanabilirsiniz:
$(this).next();
Direkt çocuktur
$('> .child-class', this)
Aşağıda ana div öğesinin tüm img öğelerini bulabilirsiniz
$(this).find('img') or $(this).children('img')
Belirli bir img öğesi istiyorsanız bu şekilde yazabilirsiniz
$(this).children('img:nth(n)')
// where n is the child place in parent list start from 0 onwards
Div'iniz yalnızca bir img öğesi içeriyor. Bunun için aşağıda doğru
$(this).find("img").attr("alt")
OR
$(this).children("img").attr("alt")
Ancak div'iniz aşağıdaki gibi daha fazla img öğesi içeriyorsa
<div class="mydiv">
<img src="test.png" alt="3">
<img src="test.png" alt="4">
</div>
ikinci img öğesinin alt değerini bulmak için üst kodu kullanamazsınız. Böylece şunu deneyebilirsiniz:
$(this).find("img:last-child").attr("alt")
OR
$(this).children("img:last-child").attr("alt")
Bu örnek, ana nesne içinde gerçek nesneyi nasıl bulabileceğinize dair genel bir fikri gösterir. Alt nesnenizi farklılaştırmak için sınıfları kullanabilirsiniz. Bu kolay ve eğlenceli. yani
<div class="mydiv">
<img class='first' src="test.png" alt="3">
<img class='second' src="test.png" alt="4">
</div>
Bunu aşağıdaki gibi yapabilirsiniz:
$(this).find(".first").attr("alt")
ve daha spesifik olarak:
$(this).find("img.first").attr("alt")
Yukarıdaki kodda olduğu gibi find veya children kullanabilirsiniz. Daha fazla bilgi için Çocuklar http://api.jquery.com/children/ adresini ziyaret edin ve http://api.jquery.com/find/ adresini bulun . Örneğe bakın http://jsfiddle.net/lalitjs/Nx8a6/
JQuery'de bir çocuğa gönderme yolları. Aşağıdaki jQuery özetledi:
$(this).find("img"); // any img tag child or grandchild etc...
$(this).children("img"); //any img tag child that is direct descendant
$(this).find("img:first") //any img tag first child or first grandchild etc...
$(this).children("img:first") //the first img tag child that is direct descendant
$(this).children("img:nth-child(1)") //the img is first direct descendant child
$(this).next(); //the img is first direct descendant child
DIV kimliğini bilmeden IMG'yi şöyle seçebilirsiniz:
$("#"+$(this).attr("id")+" img:first")
Bu kodu deneyin:
$(this).children()[0]
$($(this).children()[0]).
$(this:first-child)
$($(this).children()[x])kullanmak$(this).eq(x) ya da birincisini istiyorsanız, sadece $(this).first().
Aşağıdaki yöntemlerden birini kullanabilirsiniz:
1 bul ():
$(this).find('img');
2 çocuk ():
$(this).children('img');
jQuery's eachbir seçenektir:
<div id="test">
<img src="testing.png"/>
<img src="testing1.png"/>
</div>
$('#test img').each(function(){
console.log($(this).attr('src'));
});
Üst Selecor'u , üst öğede bulunan alt öğelere başvurmak için kullanabilirsiniz .
$(' > img', this).attr("src");
Ve aşağıda $(this)referansınız imgyoksa divve başka bir fonksiyonda bulunan referansı almak istiyorsanız .
$('#divid > img').attr("src");
İşte fonksiyonel bir kod, çalıştırabilirsiniz (basit bir gösteri).
DIV'yi tıkladığınızda görüntüyü bazı farklı yöntemlerden alırsınız, bu durumda "bu" DIV'dir.
$(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>
Umarım yardımcı olur!
İçinde 0 ila birçok <img>etiket olabilir <div>.
Bir öğe bulmak için a .find().
Kodunuzu güvende tutmak için a .each().
Birlikte .find()ve .each()birlikte kullanılması , 0 <img>eleman durumunda boş referans hatalarını önlerken birden fazla <img>elemanın kullanılmasına izin verir .
// Set the click handler on your div
$("body").off("click", "#mydiv").on("click", "#mydiv", function() {
// Find the image using.find() and .each()
$(this).find("img").each(function() {
var img = this; // "this" is, now, scoped to the image element
// Do something with the image
$(this).animate({
width: ($(this).width() > 100 ? 100 : $(this).width() + 100) + "px"
}, 500);
});
});
#mydiv {
text-align: center;
vertical-align: middle;
background-color: #000000;
cursor: pointer;
padding: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="mydiv">
<img src="" width="100" height="100"/>
</div>
$("body").off("click", "#mydiv").on("click", "#mydiv", function() {
event.stopImmediatePropagation()olmasını önlemek için elemanı kullanarak yaralandım .
$(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>
Kullanabilirsin
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
$(this).find('img');
</script>