简体   繁体   English

将标签的 href 复制到 img 标签的 src 中

[英]Copy a tag's href into img tag's src

I need a script that will copy the href attribute to a src of an image located in the same page.我需要一个脚本,将href属性复制到位于同一页面中的图像的src It also make a change in the s1600-h value of the a href address after copying it to the img src.将a href地址的s1600-h值复制到img src后,也会对其进行修改。 Here's an example:下面是一个例子:

I want the script to change this我希望脚本改变这一点

<a href="**XXX/s1600-h/XXX.jpg**"><img title="Big_3" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; float: left; padding-top: 0px; padding-left: 0px; margin: 0px 10px 10px 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="Big_3" src="**YYY.jpg?imgmax=800**" width="119" align="left" height="89"></a>

...into this ...进入这个

<a href="**XXX/s1600-h/XXX.jpg**"><img title="Big_3" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; float: left; padding-top: 0px; padding-left: 0px; margin: 0px 10px 10px 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="Big_3" src="**XXX/s200/XXX.jpg**" width="119" align="left" height="89"></a>

Using jquery you can do this使用 jquery 你可以做到这一点

url = $('img').parent().attr('href');
$('img').attr('src',url);

you can add this code with some event handler.您可以使用一些事件处理程序添加此代码。 first line of code is extracting href from anchor tag and second tag is inserting it into img tag's src.第一行代码是从锚标签中提取href,第二行是将它插入到img 标签的src 中。 This code is only a basic idea to it.这段代码只是它的一个基本概念。 You can customize it as your need.您可以根据需要自定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM