如何通过 jQuery 嵌套锚链接?
正在寻找一种在 H3 模式和最后一个 p 元素块之后在 HTML 文档中添加锚链接的好方法。
这是我原来的 HTML
<div id="container">
<h3 id="faqa">title</h3>
<p>content</p>
<h3 id="faqb">title</h3>
<p>content</p>
<p>content</p>
<h3 id="faqc">title</h3>
<p>content</p>
<p>content</p>
<p>content</p>
<p>content</p>
<h3 id="faqd">title</h3>
<p>content</p>
</div>
我想要...
<div id="container">
<h3 id="faqa">title</h3>
<p>content</p>
<p align="right"><a href="#">back to top</a>
<h3 id="faqb">title</h3>
<p>content</p>
<p>content</p>
<p align="right"><a href="#">back to top</a>
<h3 id="faqc">title</h3>
<p>content</p>
<p>content</p>
<p>content</p>
<p>content</p>
<p align="right"><a href="#">back to top</a>
<h3 id="faqd">title</h3>
<p>content</p>
<p align="right"><a href="#">back to top</a>
</div>
这是迄今为止我诚实的努力,但到目前为止还没有答案...
$("#container").each (function() {
if($(this).find('h3[id*="faq"]')){
var $mpage = window.location.pathname;
$(this).find("p:last").append('<p align="right"><a href="'+$mpage+'">Back to top</a>
</p>');
}
});
jQuery API 在前置和附加方面有一个很好的指南,但在这种特定情况下它们都没有帮助我。感谢您对情况的任何了解,我必须添加大约 40 个锚点;(
再次感谢您的帮助!
Looking for a great way to add anchor links within my HTML document after my pattern of H3 and the last p element block.
This is my original HTML
<div id="container">
<h3 id="faqa">title</h3>
<p>content</p>
<h3 id="faqb">title</h3>
<p>content</p>
<p>content</p>
<h3 id="faqc">title</h3>
<p>content</p>
<p>content</p>
<p>content</p>
<p>content</p>
<h3 id="faqd">title</h3>
<p>content</p>
</div>
And I want...
<div id="container">
<h3 id="faqa">title</h3>
<p>content</p>
<p align="right"><a href="#">back to top</a>
<h3 id="faqb">title</h3>
<p>content</p>
<p>content</p>
<p align="right"><a href="#">back to top</a>
<h3 id="faqc">title</h3>
<p>content</p>
<p>content</p>
<p>content</p>
<p>content</p>
<p align="right"><a href="#">back to top</a>
<h3 id="faqd">title</h3>
<p>content</p>
<p align="right"><a href="#">back to top</a>
</div>
Here is my honest effort so far, but no answer as of yet...
$("#container").each (function() {
if($(this).find('h3[id*="faq"]')){
var $mpage = window.location.pathname;
$(this).find("p:last").append('<p align="right"><a href="'+$mpage+'">Back to top</a>
</p>');
}
});
The jQuery API has an excellent guide on prepend and append, but neither of them help me in this specific case. Thanks for any light on the situation, I got about 40 of these anchors I have to add ;(
Thanks again for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该可以解决问题。
DEMO
更新: karim 的回答很好,很简短< del>你应该选择这个 编辑:显然,尽管答案看起来不错,但它没有添加到最后一个标题的最后一段的链接。
我仍然会在这里留下这个答案,因为它展示了解决问题的不同方法。
karim 的思维方式基本上是:获取每个
h3
并添加链接(如果它前面有一个段落)。我的方法是:获取每个
h3
并找到最后一段(在下一个h3
之前)并附加链接。他向上思考,我向下思考;)
should do the trick.
DEMO
Update: karim's answer is nice and short
and you should go with this oneEdit: Apparently, as nice as the answer seems, it does not add a link to the last paragraph of the last heading.I will still leave this answer here, as it shows a different way of approaching the problem.
karim's mindset basically is: Take every
h3
and add the link if it was preceded by a paragraph.Mine was: Take every
h3
and find the last following paragraph (before the nexth3
) and append the link.He thought upwards, I downwards ;)
怎么样:
演示。
根据我们尊敬的@Felix Kling的输入进行编辑:
演示。
How about:
Demo.
EDIT based on our esteemed @Felix Kling's input:
Demo.
演示: http://jsfiddle.net/TDSSd/
Demo: http://jsfiddle.net/TDSSd/