基于 JavaScript 的面包屑导航
我有 HTML 代码:
<ul class="breadcrumb">
<li><a href="#">Home</a> <span class="divider">/</span></li>
<li><a href="#">Library</a> <span class="divider">/</span></li>
<li class="active"><a href="#">Data</a></li>
</ul>
我想通过 JavaScript 基于 javascript $currentPath 变量来更改它,该变量存储如下字符串:“/var/www/stackoverflow” 因此,在上面的示例中,我的面包屑应该如下所示:
<ul class="breadcrumb">
<li><a href="#">var</a> <span class="divider">/</span></li>
<li><a href="#">www</a> <span class="divider">/</span></li>
<li class="active"><a href="#">stackoverflow</a></li>
</ul>
有人可以帮助我吗?
I have HTML code:
<ul class="breadcrumb">
<li><a href="#">Home</a> <span class="divider">/</span></li>
<li><a href="#">Library</a> <span class="divider">/</span></li>
<li class="active"><a href="#">Data</a></li>
</ul>
which I want to be changed by JavaScript based on javascript $currentPath variable which stores string like this: "/var/www/stackoverflow"
So in example above my breadcrumbs should look like:
<ul class="breadcrumb">
<li><a href="#">var</a> <span class="divider">/</span></li>
<li><a href="#">www</a> <span class="divider">/</span></li>
<li class="active"><a href="#">stackoverflow</a></li>
</ul>
Could someone help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以像这样分割变量。
you can split the variable like this.
为您概述该过程:
'/'
上拆分
$currentPath
字符串。这会给你一个(面包屑)数组。根据您的喜好创建
(或克隆
)附加
您的新列表。
如果您需要详细信息,请随时询问,否则请尽情解决;)
Google:
mdn split
jquery clone
jquery text
jquery append
To outline the process for you:
split
the$currentPath
string on'/'
. This gives you an array (of crumbs).for each
element in the array:create
(orclone
) a<li>
-element to your liking, setting its contents as you need.append
your new<li>
the<ul>
list.Feel free to ask if you want details, otherwise enjoy working it out ;)
Google:
mdn split
jquery clone
jquery text
jquery append