我需要使用jQuery创建与第一个url相同的第二个url。请参阅下面的html代码。它是div
和a
s。
<div class="text-center">
<h5 class="category"><a href="url" rel="tag">No need to change this url</a> </h5>
<div class="tx-div small"></div>
<a href="url1"><p class="name">Url</p></a>
<span class="price">
<div class="add-to-cart-button">
<a href="url2" rel="nofollow" class="add_to_cart_button product_type_simple button alt-button small clearfix" target="_blank">Change this to url1 </a>
</div>
</div>
<div class="text-center">
<h5 class="category"><a href="url" rel="tag">No need to change this url</a> </h5>
<div class="tx-div small"></div>
<a href="url1"><p class="name">Url</p></a>
<span class="price">
<div class="add-to-cart-button">
<a href="url2" rel="nofollow" class="add_to_cart_button product_type_simple button alt-button small clearfix" target="_blank">Change this to url1 </a>
</div>
</div>
请帮忙
答案 0 :(得分:1)
更新代码:
$('.text-center').each(function(){
$('a:eq(2)', this).attr('href', $('a:eq(1)', this).attr('href'));
});
试试这段代码:
$('.fc').each(function(){
$('.sec', this).attr('href', $('.fic', this).attr('href'));
});
答案 1 :(得分:1)
试试这段代码,这是一个有效的例子试试....
$(".text-center").find('.url1').each(function( i ) {
var url1 = $(this).attr('href');
$(this).parent().find('.url2').attr('href',url1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="text-center">
<h5 class="category"><a href="url" rel="tag">No need to change this url</a> </h5>
<div class="tx-div small"></div>
<a class="url1" href="url1"><p class="name">Url</p></a>
<span class="price">
<div class="add-to-cart-button">
<a href="url2" rel="nofollow" class="add_to_cart_button product_type_simple button alt-button small clearfix url2" target="_blank">Change this to url1 </a>
</div>
</span>
</div>
<div class="text-center">
<h5 class="category"><a href="url" rel="tag">No need to change this url</a> </h5>
<div class="tx-div small"></div>
<a class="url1" href="url11111"><p class="name">Url</p></a>
<span class="price">
<div class="add-to-cart-button">
<a href="url22222" rel="nofollow" class="add_to_cart_button product_type_simple button alt-button small clearfix url2" target="_blank">Change this to url1 </a>
</div>
</span>
</div>