简体   繁体   English

使用Javascript / jQuery基于当前页面和列表的动态面包屑()

[英]Dynamic breadcrumb based on current page & list using Javascript/jQuery ()

found these two approaches to realize breadcrumbs: 发现了以下两种实现面包屑的方法:

The later one is almost the thing I'm looking for but has three issues: 后一个几乎是我正在寻找的东西,但有三个问题:

  1. it fails if there a serveral pages with the same name 如果有相同名称的服务器页面将失败
  2. the current page has a link 当前页面有一个链接
  3. I would like to remove the first ie Home-link 我想删除第一个,即首页链接

Could you please help me to improve that, I'm a total noob... ? 您能帮我改善一下吗,我真是​​个菜鸟……?

Thats the code which I took from the other post: 那就是我从另一篇文章中获取的代码:

var url = "level3.html";
//location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
var currentItem = $(".items").find("[href$='" + url + "']");
$(".bredcrumb").html($("<a href='/'>Home</a>"));
$(currentItem.parents("li").get().reverse()).each(function () {
    $(".bredcrumb").append("/").append( $(this).children("a"));
});

The list: 名单:

<nav class="items">
    <ul>
        <li><a href="test.html">Test 1</a>
        </li>
        <li><a href="test2.html">Test 2</a>
            <ul>
                <li><a href="level1.html">Level 1</a>
                </li>
                <li><a href="test/level2.html">Level 2</a>
                    <ul>
                        <li><a href="test/level2/level3.html">Level 3</a>
                        </li>
                        <li><a href="test/level2/level32.html">Also at level 3</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><a href="test3.html">Test 3</a>
        </li>
    </ul>
</nav>
<div class="bredcrumb"></div>

I changed location.pathname.substring(location.pathname.lastIndexOf("/") + 1); 我更改了location.pathname.substring(location.pathname.lastIndexOf(“ /”)+1); into location.pathname.substring(location.pathname.lastIndexOf("/") - 3); location.pathname.substring(location.pathname.lastIndexOf(“ /”)-3); which seems to help for the first issue. 这似乎有助于解决第一个问题。 But I'm not sure whether that is a good solution. 但是我不确定这是否是一个好的解决方案。

Thank you very much, Tobias 非常感谢Tobias

The code you are looking for is 您正在寻找的代码是

var url = "level3.html";
//location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
var currentItem = $(".items").find("[href$='" + url + "']");
$(".bredcrumb").html($("Home"));
$(currentItem.parents("li").get().reverse()).each(function () {
    $(".bredcrumb").append("/").append( $(this).children("a"));
});

This removes the current page link. 这将删除当前页面链接。 if we understand the code a bit url is capturing the url of the page from which this code is extracting the last part which is assumed to be the html page name (ex: home.html). 如果我们理解该代码,则有一点url将捕获该页面的URL,此代码将从该URL中提取最后一个假定为html页面名称的部分(例如:home.html)。 then the currentItem finding the html element that has the html page name found earlier.the last line does two things 1. get the parent element of the anchor tag where you get the html page name and then append the child name of the parent element in the breadcrumb. 然后currentItem查找具有较早找到的html页面名称的html元素。最后一行做了两件事:1.在获取html页面名称的位置获取anchor标签的父元素,然后在其中附加父元素的子名称。面包屑。 this is so because the page name and the appearance int he menu is not always same example you name the page as myhmpg.html but in the menu u might have shown it as Home. 之所以这样,是因为页面名称和菜单的外观并不总是相同的,例如,您将该页面命名为myhmpg.html,但是在菜单中您可能会将其显示为Home。 Hope this explanation helps. 希望这种解释有所帮助。 Please comment back you see your question is not answered properly 请回覆您看到的问题未正确回答

I finally found the (dirty?) answer to the problem: 我终于找到了这个问题的答案(肮脏?):

    $(document).ready(function() {  
        var url = location.href; //absolute path instead of file name
        var currentItem = $(\".items\").find(\"[href$='\" + url +  \"']\");
        $(\".bredcrumb\").html($(\"Home\"));        
        $(currentItem.parents(\"li\").get().reverse()).each(function () {
          $(\".bredcrumb\").append( $(this).children(\"a\"));
        });
    });

Then I hided the last-child with css and took a css border to divide the breadcrumbs. 然后我用css隐藏了最后一个孩子,并拿了一个css边框来划分面包屑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM