2010-09-08 15 views
6

के साथ jQuery रैप आईएमजी हैलो, अगर कक्षा में एंकर है तो मैं एंकर के साथ एक छवि को लपेटने की कोशिश कर रहा हूं।एंकर

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> 

इसके अलावा, मैं एक डबल या एकल उद्धरण का उपयोग कर किया जाना चाहिए?

उत्तर

7

यह कैसे है?

$("img.anchor").wrap('<a href="#" />'); 

रेफरी:

$('.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>'); 
    } 
}); 

यह एक लंगर ही href साथ .view-field-field-node-ref-id a रूप img आसपास .views-field-field-teaserbox-fid में लपेट जाएगा: http://api.jquery.com/wrap/

0

मेरा अनुमान है कि आप कुछ इस तरह कर सकते हैं।

ओह, और लगभग एकल या दोहरे उद्धरण: इससे कोई फर्क नहीं पड़ता - आपके लिए जो कुछ भी काम करता है उसका उपयोग करें।

+0

'.wrapAll()' फ़ंक्शन के बाईं ओर तत्व को लपेटता है, इसलिए इससे कुछ अमान्य HTML बन जाएगा :) –

+0

@ निक ओप्स ...। –

+0

यी, मैंने आपके कोड की कोशिश की और यह काम नहीं कर रहा है। मैं – arkjoseph

संबंधित मुद्दे