【发布时间】:2016-09-26 20:02:30
【问题描述】:
我必须为许多 html href 属性(一个标签)分配紧随其后的各自 id 的值。
使用我的代码,我只得到一个值(第一个)。
非常感谢。
朱里
$(".anchor").attr("href","#"+($(".anchor").closest("div").next("div").attr("id")));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<a class="anchor">Get anchor 1</a>
</div>
<div id="link1">
<p>A paragraph</p>
</div>
<div>
<a class="anchor">Get anchor 2</a>
</div>
<div id="link2">
<p>Another paragraph</p>
</div>
【问题讨论】:
-
您的问题解决了吗?
-
@Mohammad 是的,非常感谢,你们都是最棒的
标签: javascript jquery html attributes href