添加图片链接

时间:2014-11-10 20:11:37

标签: jquery ajax json

我需要为图像添加超链接。这是我的代码。如何添加图像链接?

success: function(data, textStatus, jqXHR){
  $.each( data, function( idx, obj ) {
  //   $( "<img>" ).attr( "src", '/images/' + obj.Icon ).attr("title", obj.DisplayName).appendTo( "#images" );
});

2 个答案:

答案 0 :(得分:0)

假设obj还包含链接地址,并假装obj.Href就在其中:

$.each( data, function( idx, obj ) {
  var i = $( "<img>" ).
    attr( "src", '/images/' + obj.Icon );

  $('a').
    attr('href', obj.Href).
    attr("title", obj.DisplayName).
    append(i).
    appendTo( "#images" );
});

答案 1 :(得分:0)

你可以这样做:

$.each(data, function (idx, obj) {
    $("<img>").attr({
        src: '/images/' + obj.Icon,
        title: obj.DisplayName,
    }).appendTo("#images").wrap('<a href="url">');
});

http://jsfiddle.net/py25pL3c/