我有一个产品清单.每个产品都有标题和评论链接.目前,标题直接链接到单个产品页面,评论链接转到其他位置.
我想使用jquery每个循环遍历每个li,从标题中获取href(第一个链接),并将其应用于审阅链接(第二个链接),因此它们都指向产品页面.
简化的代码如下:
<ul>
<li><a href="product1.html">Product 1</a><a href="review1.html">Review 1</a></li>
<li><a href="product2.html">Product 2</a><a href="review2.html">Review 2</a></li>
<li><a href="product3.html">Product 3</a><a href="review3.html">Review 3</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我认为它会像下面这样:
$("li").each(function(){
var link = $("a:eq(0)").attr('href');
$("a:eq(1)").attr("href", link);
});
Run Code Online (Sandbox Code Playgroud)
但它总是使用相同的变量"链接".
有人可以帮我吗?
Gab*_*abe 11
我this作为参数传递,以定义each()循环的每次迭代的上下文.在每次迭代中,this引用相关元素.
$("li").each(function(){
var link = $("a:eq(0)", this).attr('href');
$("a:eq(1)", this).attr("href", link);
});
Run Code Online (Sandbox Code Playgroud)