gpt4 book ai didi

javascript - jQuery:查找 H2 id 并附加具有相同 id 的链接

转载 作者:行者123 更新时间:2023-11-28 14:54:21 25 4
gpt4 key购买 nike

我有以下代码,我希望为每个具有 id 属性的 H2 创建一个 anchor 链接。我无法让它正常工作...

<div id="mydiv">
<h2 id="first">first</h2>
<h2>second</h2>
<h2 id="third">third</h2>
</div>

.sls {color: red;font-size: 12px;}

$('#mydiv')
.find('h2[id]')
.append(' <a href="http://www.test.com/page.html#'+ $('h2[id]')
.attr('id') +'" class="sls">Link</a>');

<强> https://jsfiddle.net/ivanionut/1ohh2hws/

感谢您的帮助。

最佳答案

你想要这样的东西吗?

$('#mydiv').find('h2[id]').each(function () {
$(this).append(' <a href="http://www.test.com/page.html#'+ $(this).attr('id') +'" class="sls">Link</a>');
});
.sls {
color: red;
font-size: 12px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mydiv">
<h2 id="first">first</h2>
<h2>second</h2>
<h2 id="third">third</h2>
</div>

关于javascript - jQuery:查找 H2 id 并附加具有相同 id 的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43442452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com