使用 jQuery 缩短面包屑导航

发布于 2024-11-06 06:01:45 字数 705 浏览 0 评论 0原文

我的面包屑有以下代码:

<a name="top"></a>
<a href="/index.html">Home</a>&nbsp;/&nbsp;
<a href="../../../../index.html">Level1</a>&nbsp;/&nbsp;
<a href="../../../index.html">Level2</a>&nbsp;/&nbsp;
<a href="../../index.html">Level3</a>&nbsp;/&nbsp;
<a href="../index.html">Level4</a>&nbsp;/&nbsp;
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

庆幸我还是我 2024-11-13 06:01:45

尝试这个有点复杂,但应该可以工作:

 <style type="text/css">
    .breadcrums 
    {
        width:500px;
    }
    .breadcrums li
    {
        list-style-type:none;
        display:inline;
    }
    </style>
    <script type="text/javascript">
        $(function () {
        var length =  $('.breadcrums li').length;
        $('.breadcrums li').each(function(n){
                if(n > 2 && n!=length-1){
                $(this).hide();
                }
            });        
        });
    </script>

<ul class="breadcrums">
<li> <a name="top"></a></li>
<li><a href="/index.html">Home</a> / </li>
<li><a href="../../../../index.html">Level1</a> / </li>
<li><a href="../../../index.html">Level2</a> / </li>
<li><a href="../../index.html">Level3</a> / </li>
<li><a href="../index.html">Level4</a> / </li>
<li>Page</li>
</ul>

更新 1:
将链接标签括在 li 中的主要原因是隐藏 / 分隔符。如果不附上它,我不确定您将如何获得该文本。

检查此版本是否满足您的需求:

<div class="wayfinding">
<a name="top"></a>
<a href="/index.html">Home</a><span> / </span>
<a href="../../../../index.html">Level1</a><span> / </span>
<a href="../../../index.html">Level2 </a><span> / </span>
<a href="../../index.html">Level3 </a><span> / </span>
<a href="../index.html">Level4 </a><span> / </span>
Page
</div>

var length = $('.wayfinding a').length;
            $('.wayfinding a').each(function (n) {
                if (n > 2 && n != length) {
                    $(this).next().hide();
                    $(this).hide();
                }
                if (n == 2) {
                     $(this).next().after('<span>[...] / </span>');
                }
            });

Try this a bit complex but should work:

 <style type="text/css">
    .breadcrums 
    {
        width:500px;
    }
    .breadcrums li
    {
        list-style-type:none;
        display:inline;
    }
    </style>
    <script type="text/javascript">
        $(function () {
        var length =  $('.breadcrums li').length;
        $('.breadcrums li').each(function(n){
                if(n > 2 && n!=length-1){
                $(this).hide();
                }
            });        
        });
    </script>

<ul class="breadcrums">
<li> <a name="top"></a></li>
<li><a href="/index.html">Home</a> / </li>
<li><a href="../../../../index.html">Level1</a> / </li>
<li><a href="../../../index.html">Level2</a> / </li>
<li><a href="../../index.html">Level3</a> / </li>
<li><a href="../index.html">Level4</a> / </li>
<li>Page</li>
</ul>

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:

<div class="wayfinding">
<a name="top"></a>
<a href="/index.html">Home</a><span> / </span>
<a href="../../../../index.html">Level1</a><span> / </span>
<a href="../../../index.html">Level2 </a><span> / </span>
<a href="../../index.html">Level3 </a><span> / </span>
<a href="../index.html">Level4 </a><span> / </span>
Page
</div>

var length = $('.wayfinding a').length;
            $('.wayfinding a').each(function (n) {
                if (n > 2 && n != length) {
                    $(this).next().hide();
                    $(this).hide();
                }
                if (n == 2) {
                     $(this).next().after('<span>[...] / </span>');
                }
            });
℡Ms空城旧梦 2024-11-13 06:01:45

您可以添加选择器或修改 HTML 吗?包裹每一层面包屑将使这更容易完成。

改变 HTML

<div id="wayfinding">
    <a name="top"></a>
    <div class="level"><a href="/index.html">Home</a></div>
    <div class="level"><a href="../../../../index.html">Level1</a> / </div>
    <div class="level"><a href="../../../index.html">Level2</a> / </div>
    <div class="level"><a href="../../index.html">Level3</a> / </div>
    <div class="level"><a href="../index.html">Level4</a> / </div>
    Page
</div>

JS

$('#wayfinding .level:not(:first)').remove();
$('#wayfinding .level:last').after('[...]');

Can you add selectors or modify the HTML at all? Wrapping each level of breadcrumb would make this much easier to accomplish.

Altered HTML

<div id="wayfinding">
    <a name="top"></a>
    <div class="level"><a href="/index.html">Home</a></div>
    <div class="level"><a href="../../../../index.html">Level1</a> / </div>
    <div class="level"><a href="../../../index.html">Level2</a> / </div>
    <div class="level"><a href="../../index.html">Level3</a> / </div>
    <div class="level"><a href="../index.html">Level4</a> / </div>
    Page
</div>

JS

$('#wayfinding .level:not(:first)').remove();
$('#wayfinding .level:last').after('[...]');
迷离° 2024-11-13 06:01:45

这是可行的,尽管不可否认这不是最漂亮的解决方案:

if ($('#wayfinding a').length > 3) {
    var fullPath = $('#wayfinding').html(), breadcrumbs = fullPath, w = $('#wayfinding a').length;
    for (i=0;i<=w-1;i++) {
        breadcrumbs = breadcrumbs.replace('</a>','QQQQQ');
        if (i==1) { breadcrumbs = breadcrumbs.replace('</a> / ','</a> / <span class="hiddenCrumbs">'); }
        if (i==w-2) { breadcrumbs = breadcrumbs.replace('</a>','</a></span>'); }
        if (i==w-1) { breadcrumbs = breadcrumbs.replace(/QQQQQ/g,'</a>'); }
    }
    $('#wayfinding').html(breadcrumbs);
    $('.hiddenCrumbs').html('[...]').click(function() {
        $('#wayfinding').html(fullPath);
    });
}

This works, although admittedly it's not the prettiest solution:

if ($('#wayfinding a').length > 3) {
    var fullPath = $('#wayfinding').html(), breadcrumbs = fullPath, w = $('#wayfinding a').length;
    for (i=0;i<=w-1;i++) {
        breadcrumbs = breadcrumbs.replace('</a>','QQQQQ');
        if (i==1) { breadcrumbs = breadcrumbs.replace('</a> / ','</a> / <span class="hiddenCrumbs">'); }
        if (i==w-2) { breadcrumbs = breadcrumbs.replace('</a>','</a></span>'); }
        if (i==w-1) { breadcrumbs = breadcrumbs.replace(/QQQQQ/g,'</a>'); }
    }
    $('#wayfinding').html(breadcrumbs);
    $('.hiddenCrumbs').html('[...]').click(function() {
        $('#wayfinding').html(fullPath);
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文