加载新页面后,HTML 面包屑显示然后丢失位置

2023-01-26前端开发问题
3

本文介绍了加载新页面后,HTML 面包屑显示然后丢失位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的网站上的面包屑导航有问题.

I have an issue with the breadcrumb on my site.

在页面加载时,会显示主页(这是正确的).当我点击导航菜单中的链接时,面包屑会更新为:

On page load, the Home is displayed (which is correct).. When i click on a link within my navigation menu, the breadcrumb is updated with say:

首页 > 新页面

但是,当加载新页面时,面包屑会返回到仅显示主页.

However, when the New Page is loaded, the breadcrumb goes back to only displaying Home.

我的导航 JSP 文件:

My Navigation JSP file:

<nav class="nav">
  <ul class="wrap">

    <li class="menu-nav--home">
      <a href="/home" title="home">
        <span class="icon-home"></span>
      </a>
    </li>
    <ul>
      <li><a href="/business">Business</a></li>
      <li><a href="/search">Search</a></li>
      <li><a href="/details">Search Deatils</a></li>
      <li><a href="/update">Change Records</a></li>
      <li><a href="/delete">Remove</a></li>
      <li><a href="/order">Order Deatils</a></li>
      <li><a href="/checkout">Checkout</a></li>
    </ul>
  </li>
</ul>
</nav>

<div class="wrap breadcrumb">
  <div class="item">
    <a href="#home">Home </a>
  </div>
</div>

我的 jQuery 代码:

My jQuery code:

$(function() {
  createBreadcrumbs();      
}

function createBreadcrumbs() {
  $('.site-nav a').on('click', function() {
    var $this = $(this),
    $bc = $('<div class="item"></div>');

    $this.parents('li').each(function(n, li) {
      var $a = $(li).children('a').clone();
      $bc.prepend(' > ', $a);
    });

    $('.breadcrumb').html( $bc.prepend('<a href="/">Home </a>') );    
    return false;
  });

任何帮助表示赞赏.

我已尝试遵循此处的建议 - jquery - 动态面包屑

I have attempted to follow the advice found here - jquery - dynamic breadcrumb

谢谢

推荐答案

您的代码中有几个错误.首先,您忘记了以下代码中的 );:

There are a couple of errors in your code. First of all, you are forgetting a ); in the following code:

$(function() {
  createBreadcrumbs();      
} ); //HERE!

您可能忘记将最后一个 } 复制到此问题中.我在这里添加了那个.您正在将处理程序附加到作为具有 site-nav 类的元素的子元素的链接.您的代码中没有这样的东西.将其更改为 .nav a.

and you might have forgotten to copy the last } to this question. I've added that one in here. You are attaching a handler to links that are a child of an element with the site-nav class. There is no such thing in your code. Change that to .nav a.

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

    $this.parents('li').each(function(n, li) {
      var $a = $(li).children('a').clone();
      $bc.prepend(' > ', $a);
    });

    $('.breadcrumb').html( $bc.prepend('<a href="/">Home </a>') );    
    return false;
  });
}

如果您希望它实际显示某些内容,则需要使用 ajax 加载页面并将生成的 html 推送到 DOM 中.

If you want this to actually display something, you'll need to load the page with ajax and shove the resulting html into the DOM.

这篇关于加载新页面后,HTML 面包屑显示然后丢失位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5

如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20 前端开发问题
6