简体   繁体   中英

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)

Here is my 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.

Thanks in advance.

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

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

This can be easily done with jQuery.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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