简体   繁体   English

使用javascript或jQuery提取和替换href标记

[英]Extracting and replacing href tag using javascript or JQuery

I have no idea where to start with this so I was hoping someone might be able to suggest a solution. 我不知道从哪里开始,所以我希望有人能够提出解决方案。

How do I extract the contents of the href tag of "product_prev" (/images/New-Image.jpg) and use it to replace the href tag of "product" (/images/Old-Image.jpg) 如何提取“ product_prev”的href标签(/images/New-Image.jpg)的内容,并使用它替换“产品”的href标签(/images/Old-Image.jpg)

Here is my HTML... 这是我的HTML ...

<a class="product_prev" href="/images/New-Image.jpg"><img alt="" title="" src="/images/New-Image.thumb.jpg" width="50"></a>

<a class="product" href="/images/Old-Image.jpg"><img class="displayer ct_pd_pi_mainImage ct_pd_pi_fullsizeImage" alt="" title="Venicci-Mini-Pram-Black" itemprop="image" src="/images/Old-Image.jpg"></a>

I'm unable to edit the template of the site but I can add CSS/Javascript/JQuery so hopefully a solution using these methods can be used. 我无法编辑网站模板,但可以添加CSS / Javascript / JQuery,因此希望可以使用使用这些方法的解决方案。

Thanks in advance. 提前致谢。

您可以使用jquery的属性选择器来获取和设置href

$(".product").attr("href",$("product_prev").attr("href"));

This can be easily done with jQuery. 使用jQuery可以很容易地做到这一点。

var first_href= $(".product_prev").attr('href');
$(".product").attr('href', first_href);

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

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