使用 jQuery 缩短面包屑导航
我的面包屑有以下代码:
<a name="top"></a>
<a href="/index.html">Home</a> /
<a href="../../../../index.html">Level1</a> /
<a href="../../../index.html">Level2</a> /
<a href="../../index.html">Level3</a> /
<a href="../index.html">Level4</a> /
Page
生成:“Home / Level1 / Level2 / Level3 / Level4 / Page”。我想使用 javascript/jQuery 将其缩短为“Home / Level1 [...] Page”,无论其他页面有多少,主页、Level1 和页面始终显示。但我不知道如何抓住中间的东西。
仅当超过 Level1 时才会触发以下内容,但我该从这里开始做什么呢?
if ($('#wayfinding a').length > 2) {}
I have the following code for my breadcrumbs:
<a name="top"></a>
<a href="/index.html">Home</a> /
<a href="../../../../index.html">Level1</a> /
<a href="../../../index.html">Level2</a> /
<a href="../../index.html">Level3</a> /
<a href="../index.html">Level4</a> /
Page
Which produces: "Home / Level1 / Level2 / Level3 / Level4 / Page". I want to use javascript/jQuery to shorten it to "Home / Level1 [...] Page", with Home, Level1 and the Page always showing, regardless of the number of other pages. But I'm not sure how to grab the stuff inbetween.
The following will only trigger if there's more than Level1, but where do I go from here?
if ($('#wayfinding a').length > 2) {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这个有点复杂,但应该可以工作:
更新 1:
将链接标签括在 li 中的主要原因是隐藏 / 分隔符。如果不附上它,我不确定您将如何获得该文本。
检查此版本是否满足您的需求:
Try this a bit complex but should work:
Update 1:
The main reason to enclose the link tag in li is to hide the / separator. Without enclosing it, I am not sure how you would get hold of that text.
Check if this version fit your needs:
您可以添加选择器或修改 HTML 吗?包裹每一层面包屑将使这更容易完成。
改变 HTML
JS
Can you add selectors or modify the HTML at all? Wrapping each level of breadcrumb would make this much easier to accomplish.
Altered HTML
JS
这是可行的,尽管不可否认这不是最漂亮的解决方案:
This works, although admittedly it's not the prettiest solution: