gpt4 book ai didi

javascript - 从单个类渲染菜单栏并在 Yii2 中动态突出显示菜单

转载 作者:行者123 更新时间:2023-11-28 05:31:01 25 4
gpt4 key购买 nike

我的 Yii2 项目中有一个单独的顶部菜单栏。我将它们呈现在我的索引文件中并使用它们。

动态使用事件类的js文件。

$(document).ready(function () {
var url = window.location;
// Will only work if string in href matches with location
$('ul.navbark a[href="' + url + '"]').parent().addClass('active');
// Will also work for relative and absolute hrefs
$('ul.navbark a').filter(function () {
return this.href == url;
}).parent().addClass('active').parent().parent().addClass('active');
});

效果很好。它准确地分配事件类,但问题是当我在同一页面中使用搜索时。 URL 更改导致出现问题。当我提交搜索表单时,事件类将被删除。我怎样才能解决这个问题?

谢谢!!

最佳答案

我找到了问题的解决方案:

到目前为止,我还没有使用 yii2 导航栏,而是使用了 html。当我使用 yii2 Navbar 时,我不需要调用任何 javascript。

这是我的菜单栏 View 页面:views/topmenu/chemicalinventory.php

<?php echo yii\bootstrap\Nav::widget([
'items' => [
['label' => Yii::t('app','Inventory'),'url' => ['/product/index']],
['label' => Yii::t('app','Requests'),'url' => ['/product-requests/index']],
['label' => Yii::t('app','Deleted'),'url' => ['/productlines-deleted/index']],
],
'options' => ['class' =>'nav nav-tabs nav-tab nav-text navbark'],
]);
?>

现在我可以在我的任何 View 页面中呈现它。

 <?php echo \Yii::$app->view->renderFile('@app/views/topmenu/chemicalinventory.php'); ?>

我的顶部菜单的 CSS 是

.nav-tab-pills { border-bottom: 3px solid #DDD; }
.nav-tab-pills > li.active > a, .nav-tab > li.active > a:focus, .nav-tab-pills > li.active > a:hover { border-width: 0;background-color:#F7F7F7; }
.nav-tab-pills > li > a { border: none; color: gray; }
.nav-tab-pills > li.active > a, .nav-tab-pills > li > a:hover { border: none; color: #1ABB9C !important; background: transparent; }
.nav-tab-pills > li > a::after { content: ""; background: #2A3F54; height: 3px; position: absolute; width: 100%; left: 0px; bottom: -1px; transition: all 250ms ease 0s ; transform: scale(0); }
.nav-tab-pills > li.active > a::after, .nav-tab-pills > li:hover > a::after {transform: scale(1); }
.tab-nav-pills > li > a::after { background: #21527d none repeat scroll 0% 0%;}
.tab-pane { padding: 15px 0; }
.tab-content{padding:20px}
.nav-text{font-weight: bold}
.nav-text{font-size: 15px;}

这将给出如下图所示的输出:

Click Here

关于javascript - 从单个类渲染菜单栏并在 Yii2 中动态突出显示菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39698410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com