首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用javascript将IMG标记中的图像转换为<A>标记中的实际链接图像

使用javascript将IMG标记中的图像转换为<A>标记中的实际链接图像
EN

Stack Overflow用户
提问于 2012-07-05 10:36:03
回答 2查看 1.3K关注 0票数 3

我以前问过这个问题,并多次尝试解决它,但都没有成功。

作为一个对javascript几乎没有经验的非程序员,

我想有一个书签或类似的东西,帮助我用原始(全尺寸)图像替换页面上的所有图像,该图像的链接指向。例如,如果我点击链接的图像,它将带我到全尺寸图像,但我希望该全尺寸图像显示在页面上。

下面是一个站点代码的示例:

代码语言:javascript
复制
<a href="http://www.diyphysics.com/wp-content/uploads/2012/02     Multiplier_schematic.jpg">
<img class="aligncenter wp-image-627" title="Multiplier_schematic" src="http://www.diyphysics.com/wp-content/uploads/2012/02/Multiplier_schematic-1024x205.jpg" alt="Schematic of Dual-Polarity High-Voltage Cockroft-Walton Multiplier by David and Shanni Prutchi" width="614" height="123"/>
</a>

我希望它是:

代码语言:javascript
复制
<img class="aligncenter wp-image-627" title="Multiplier_schematic" src="http://www.diyphysics.com/wp-content/uploads/2012/02/Multiplier_schematic.jpg" alt="Schematic of Dual-Polarity High-Voltage Cockroft-Walton Multiplier by David and Shanni Prutchi"/>

宽度和高度限制已删除。

举个例子,这个网站

http://www.pocketmagic.net/?p=802

有许多小的低分辨率图像显示为链接,我希望书签小程序将所有这些图像替换为它们所指向的高分辨率图像。这样我就可以将页面保存为一个整体,或者将其导出...

谢谢!

编辑: 1. @Frosco,我试过了,但我对JS的了解很少,哪里也去不了。

  1. @PhD,它应该是一个带有javascript: protocol的小手册。面向用户,不适合web开发人员。

解决方法:多亏了John,我想通了,

这是JS bookmarklet,如果有人感兴趣的话:

代码语言:javascript
复制
javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"1.3.2",function($,L){$('a img').each(function(index) {    var currentElement= $(this);    var anchor = currentElement.parent();    var highResSource = anchor.attr("href");    var newImage = "<img src='" + highResSource + "'/>" +"</div>";    anchor.html(newImage);});});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-05 10:46:31

像这样的东西应该会让你开始:

代码语言:javascript
复制
$('a img').each(function(index) {
    var currentElement= $(this);
    var anchor = currentElement.parent();
    var highResSource = anchor.attr("href");
    var newImage = "<img src='" + highResSource + "'/>";
    anchor.html(newImage);
});​
票数 0
EN

Stack Overflow用户

发布于 2012-07-05 10:49:49

你可以试试.unwrap()

代码语言:javascript
复制
$('img').each(function() {
   var $this = $(this);
   if ($this.attr('src') === $this.parent('a').attr('href')) {
       $this.unwrap();
   }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11337234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档