开发者

JQuery add tag <a> around <img>

How can I add tag aroud with link to that image with J开发者_Go百科Query?


This will wrap a set of images with links to them:

$('some selector for the images').each(function() {
    $(this).wrap("<a href='" + this.src + "'/>");
});

...uses .each (link), .wrap (link), and the native DOM src (link) property for image elements.

Edit Or as Pointy points out (but not pointedly), just pass a function into wrap:

$('some selector for the images').wrap(function() {
  return "<a href='" + this.src + "'/>";
});

Live example


$('#img').each(function(){
    var $this = $(this); 
    $this.wrap('<a href="' + $this.attr('src') + '"></a>');
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜