2010-09-08 19 views
6

Witam Próbuję zawinąć obraz za pomocą kotwicy, jeśli klasa ma kotwicę.jQuery wrap img z kotwicą

jQuery:

if ($(".views-row:has(a)").length) { 
    var noderef = $(this).attr("anchor"); 
    $(".views-field-field-teaserbox-fid img").wrap("[anchor = "'+noderef+'" ]"); 
} 

HTML:

<div class="view-content"> 
     <div class="views-row"> 

      <div class="views-field-field-node-ref-nid"> 
      <span class="field-content"><a class="active" href="/all-essentials-inspiring-events">All the essentials of inspiring events.</a></span> 
     </div> 

     <div class="views-field-field-teaserbox-fid"> 
      <span class="field-content"><img width="208" height="137" src="http://localhost:8888/sites/default/files/wedding_Giveaway_teaser.jpg?1283880578" alt="" class="imagefield imagefield-field_teaserbox"></span> 
    </div> 

Również byłbym za pomocą podwójne lub pojedyncze cytaty?

Odpowiedz

0

Zgaduję, może chcesz coś takiego:

$('.views-row').each(function(){ 
    var current = $(this); 
    var anchor = current.find('a').first().attr('href'); 

    if(anchor){ 
     var wrapped = $(current.find('img')).wrap('<a href="' + anchor + '"></a>'); 
    } 
}); 

Ten zawinie kotwicy z tą samą href jak .view-field-field-node-ref-id a wokół img w .views-field-field-teaserbox-fid.

O, i o pojedynczych lub podwójnych ofertach: Nie ma znaczenia - wykorzystaj to, co dla ciebie działa.

+0

'.wrapAll()' zawija element po lewej stronie funkcji, więc spowoduje to utworzenie nieprawidłowego HTML :) –

+0

@Nick Oops .... –

+0

Yi, próbowałem twojego kodu i to nie działa. Będę kontynuować debugowanie – arkjoseph