2011-03-15 80 views
0

我写了下面的代码,在每个“image”标签之后添加“map”,其中“img”有一个usemap定义 但它似乎在工作。我检查“结果”不是空的这里ASP.net ABCService是一个Web服务。在每个<img>之后加上<img>标签使用jquery

$(document.body).ready(function() { 
$("img").each(function (i) { 
    if ($(this).attr("usemap") != "") { 
     var name = $(this).attr("usemap"); 
     name = name.substring(1, name.length); 
     var map; 
     ABCService.GetMap(name, function (result, eventArgs) { 
      alert(result); 
      $(this).after(result); 
     }); 

    } 
}); 

});

回答

0

$(this)并不意味着你认为这意味着你正在给ABCService.GetMap()函数。

试试这个:

$(document.body).ready(function() { 

    $("img").each(function (i) { 
     $img = $(this) 
     if ($img.attr("usemap")) { 
      var name = $(this).attr("usemap").substring(1); 
      ABCService.GetMap(name, function (result, eventArgs) { 
       alert(result); 
       $img.after(result); 
      }); 

     } 
    }); 
}); 

编辑:如果你只希望<map><img>标签与属性usemap然后做到这一点似乎...

$(document.body).ready(function() { 
    $("img[usemap]").each(function (i) { 
     $img = $(this) 
     var name = $(this).attr("usemap").substring(1); 
     ABCService.GetMap(name, function (result, eventArgs) { 
      alert(result); 
      $img.after(result); 
     }); 
    }); 
}); 
+0

感谢这工作,但有是它添加的地图到所有图像的末尾的一个问题,例如 Quantbuff 2011-03-15 20:39:29

+0

这是因为这就是代码的作用!它会在页面上的所有'img'标签之后添加任何“结果”。 – 2011-03-15 20:42:55

+0

好的,但我希望它出现在每个标签之后,而不是标签。如何做到这一点..谢谢! – Quantbuff 2011-03-15 20:49:31