在每个<img>之前添加元素<a>

zpp*_*nto 0 javascript jquery

我有这个由javascript文件创建的HTML代码,这是不可理解的,因为它是使用GWT(Google Web Toolkit)创建的.

<img style="z-index: 6; width: 116px; position: absolute; left: 84px; top: 174px;" src="spiral/sample9.jpg">  
<img style="z-index: 12; width: 197px; position: absolute; left: 0px; top: 161px;" src="spiral/sample1.jpg">
<img style="z-index: 1; position: absolute; left: 659px; top: 213px; width: 85px; opacity: 0.04; display: none;" src="spiral/sample17.jpg">  
<img style="z-index: 1; position: absolute; left: 644px; top: 208px; width: 85px; opacity: 0.08; display: none;" src="spiral/sample18.jpg">
Run Code Online (Sandbox Code Playgroud)

现在我需要<a>在每个图像之前添加标记,使用as作为图像的src一部分src.例如:" href="../tmp.php?title=sample9.mp4"

任何线索我怎么能这样做?

我想要创建的输出是这样的:

<a href="../tmp.php?title=sample9.mp4"><img style="z-index: 6; width: 116px; position: absolute; left: 84px; top: 174px;" src="spiral/sample9.jpg"></a>
Run Code Online (Sandbox Code Playgroud)

tec*_*bar 6

使用jQuery,您可以使用wrap()这样做:

$('img').each(function() {
    $(this).wrap($('<a/>', {href: this.src}));
});
Run Code Online (Sandbox Code Playgroud)

  • 您可以将函数传递给`wrap()`. (2认同)
  • @Kolink帖子被标记为jQuery; 我不认为这个对于模因来说是重要的. (2认同)
  • 由于jQuery标签,我发布了这个答案****.你现在通过删除jQuery标签有效地**我的答案失效了.: - | (2认同)