【问题标题】:dynamic breadcrumbs responding with a sidebar使用侧边栏响应的动态面包屑
【发布时间】:2014-07-04 15:01:15
【问题描述】:

我有一个侧边栏来过滤带有一些参数的人,比如城市和性别。我需要面包屑,这样当我点击性别或城市时,它会破坏新的面包屑,但它还必须保存以前的参数。像这样:

Home / sex / male / city / NY

但我有一个执行此操作的 jquery 代码:

Home  / Sex /male (and when i click on city it does this:)

Home / City / NY (it erase the other)

我把代码留在这里:

  $('.items label').on('click', function() {
  var $this = $(this),
  $bc = $('<div class="item"></div>');

  $this.parents('li').each(function(n, li) {
      var $a = $(li).children('label').clone();
      ($a.addClass("btn btn-default")).removeAttr("style margin-left");
      $bc.prepend($a);
  });
    $('.btn-breadcrumb').html( $bc.prepend('<a href="#Home" class="btn btn-default"><i class="glyphicon glyphicon-home"></i></a>') );
    return false;
})

http://jsfiddle.net/mPsez/3/ 这是我从这个页面得到的一个例子。这段代码的问题和我一样;当您单击 test1、test2 或 test3 时,它会删除前一个。

【问题讨论】:

  • 看起来它对我来说工作正常。

标签: jquery html css twitter-bootstrap breadcrumbs


【解决方案1】:

试试这个:

//If we click on the anchor...
$('.items a').on('click', function () {
    var $this = $(this),
        $bc = $(".breadcrumb");

    //We store the current path on the breadcrumb.
    var previousValue = $bc.html();

    //We delete the content of the breadcrumb.
    $bc.html("");

    //For each li item...
    $this.parents('li').each(function (n, li) {
        //we make a copy of the child anchor as we will want the same behaviour on our
        //breadcrumb.
        var $a = $(li).children('a').clone();
        //and we attach the clone after the previous clone if there is any. We also
        //insert a separator.
        $bc.append(' / ', $a);
    });

    //Once we are done we attach our old value to the beginning of the breadcrumb.
    $bc.prepend(previousValue);

    //and we cancel the default behaviour of the anchor.
    return false;
})

基本上我存储了以前的值,删除了面包屑并将旧值与新值结合起来。

【讨论】:

  • 谢谢马里奥,但是你能用我有的参数替换参数吗,因为我是使用 jquery 的新手,我不知道我必须替换什么。
  • 嗯,重要的变化是你存储旧值并将其与新值合并的部分,但我给你的所有代码都完全替换了你的。而是将侦听器应用于标签,就像我应用于锚点并更改菜单的布局一样。
猜你喜欢
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多