2016-11-04 2 views
-4

SoGet Ich habe Geschwister Element eines Bildes

<a href="1.html"> 
    <img src = "image.jpg" class = "picture"/> 
    <div class="desc"><p>Brief Description</p></div> 
</a> 

<a href="2.html"> 
    <img src = "image2.jpg" class = "picture"/> 
    <div class="desc"><p>Brief Description</p></div> 
</a> 

Wie fahre ich durch die Bilder und ändern entsprechend den div?

Bisher habe ich so etwas wie:

var pictures = $('.picture'); 
(var a = 0; a < pictures.size(); a++){ 
    var description = (pictures.get(a)) (.siblings?)(.next?); 
    //Do what I want with the description 
} 

Antwort

1

Sie JQuery .each() eine Schleife durch die Elemente verwenden können. Und .siblings(), um die Geschwister dieses Elements zu erhalten.

$('.picture').each(function(){ var description = $(this).siblings('div. desc').text(); // Do what I want with the description });

0

Wie wäre es <p> direkt die Beschreibung der Auswahl?

So:

$('.picture .desc p'). ...

1

könnten Sie next verwenden.

$('.picture').each(function(idx, picture){ 
    $(picture).next('.desc'); //this will select div.desc 
}); 
1

Hinweis, dass .get(a) finden Sie eine jQuery Satz nicht geben, sondern ein einfaches Objekt https://api.jquery.com/jquery.get/

var pictures = $('.picture'); 
 
pictures.each(function() { 
 
    var description = $(this).next().html(); 
 
    //Do what I want with the description 
 
    console.log(description); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<a href="1.html"> 
 
    <img src = "image.jpg" class = "picture"/> 
 
    <div class="desc"><p>Brief Description 1</p></div> 
 
</a> 
 

 
<a href="2.html"> 
 
    <img src = "image2.jpg" class = "picture"/> 
 
    <div class="desc"><p>Brief Description 2</p></div> 
 
</a>

Verwandte Themen