使用jQuery缩短面包屑

时间:2011-05-11 22:35:13

标签: jquery jquery-selectors breadcrumbs

我的面包屑有以下代码:

<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”,无论其他页面的数量如何,Home,Level1和Page始终显示。但是我不确定如何抓住它们之间的东西。

如果超过Level1,以下内容才会触发,但我从哪里开始?

if ($('#wayfinding a').length > 2) {}

3 个答案:

答案 0 :(得分:1)

尝试这一点有点复杂但应该有效:

 <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>&nbsp;/&nbsp;</li>
<li><a href="../../../../index.html">Level1</a>&nbsp;/&nbsp;</li>
<li><a href="../../../index.html">Level2</a>&nbsp;/&nbsp;</li>
<li><a href="../../index.html">Level3</a>&nbsp;/&nbsp;</li>
<li><a href="../index.html">Level4</a>&nbsp;/&nbsp;</li>
<li>Page</li>
</ul>

更新1: 将链接标记括在li中的主要原因是隐藏/分隔符。如果没有附上,我不确定你会如何掌握这个文本。

检查此版本是否符合您的需求:

<div class="wayfinding">
<a name="top"></a>
<a href="/index.html">Home</a><span>&nbsp;/&nbsp;</span>
<a href="../../../../index.html">Level1</a><span>&nbsp;/&nbsp;</span>
<a href="../../../index.html">Level2 </a><span>&nbsp;/&nbsp;</span>
<a href="../../index.html">Level3 </a><span>&nbsp;/&nbsp;</span>
<a href="../index.html">Level4 </a><span>&nbsp;/&nbsp;</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>[...]&nbsp;/&nbsp;</span>');
                }
            });

答案 1 :(得分:0)

您可以添加选择器或修改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>&nbsp;/&nbsp;</div>
    <div class="level"><a href="../../../index.html">Level2</a>&nbsp;/&nbsp;</div>
    <div class="level"><a href="../../index.html">Level3</a>&nbsp;/&nbsp;</div>
    <div class="level"><a href="../index.html">Level4</a>&nbsp;/&nbsp;</div>
    Page
</div>

JS

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

答案 2 :(得分:0)

虽然这不是最漂亮的解决方案,但确实有效:

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>&nbsp;/&nbsp;','</a>&nbsp;/&nbsp;<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);
    });
}