开发者

jQuery wrap img with anchor

开发者 https://www.devze.com 2023-01-15 09:57 出处:网络
Hello I have trying to wrap an ima开发者_开发知识库ge with anchor if class has anchor. jQuery:

Hello I have trying to wrap an ima开发者_开发知识库ge with anchor if class has anchor.

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>

Also, should I be using a double or single quotes?


How's this?

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

ref: http://api.jquery.com/wrap/


I'm guessing you might want something like this:

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

This will wrap an anchor with the same href as .view-field-field-node-ref-id a around the img in .views-field-field-teaserbox-fid.

Oh, and about single or double quotes: It doesn't matter - use whatever works for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消