简体   繁体   English

切换导航栏活动元素

[英]toggle the nav-bar active element

I'm using blade template, the template contains a navigation bar. 我正在使用刀片模板,该模板包含一个导航栏。 it is something like this 是这样的

<ul>
    <li class="active"><a href="page1">page1</a></li>
    <li><a href="page1">page1</a></li>
    <li><a href="page1">page1</a></li>
</ul> 

Using jquery I can make the li element active once clicked. 使用jquery,我可以使li元素一旦被激活即可。 Now the problem is when I click on the second link the page2 will be loaded and that page extends the same template so it will load it again and there then the 1st element will be active. 现在的问题是,当我单击第二个链接时,将加载page2,并且该页面扩展了相同的模板,因此它将再次加载它,然后第一个元素将处于活动状态。 The solution that I thought about is adding a div to each page to let me identify the page 我考虑过的解决方案是在每个页面上添加一个div,以便我识别该页面

<div class="type" data-type="page2"></div> 

and when the page is loaded I set the selected li element depending on the page type. 当页面加载时,我根据页面类型设置选定的li元素。 I try this and it is working the only thing is that I don't think it is the perfect solution. 我尝试了这一点,它起作用了,唯一的是我认为这不是完美的解决方案。 Is there an other more simple way to solve this? 还有其他更简单的方法来解决这个问题吗? Thanks 谢谢

I'd set a class on the html element to identity your page. 我会在html元素上设置一个class来标识您的页面。 That way you can have javascript as well as css react to what type of page you're on. 这样,您就可以让javascript和CSS对您所处的页面类型做出反应。 Not that you need css in this particular example, but down the line use-cases might pop-up and then you already have a solution in place. 在这个特定的示例中,并不是说您需要CSS,而是可能突然弹出用例,然后您已经有了一个解决方案。

edit : 编辑

Adding class dynamically through a tiny script: 通过一个小的脚本动态添加class

    //script in specific page
    <script>pagetype = "page2"</script>

    //script as part of layout template shared between pages
    <script>
    $(function(){
      $("html").addClass(pagetype);
    })
    </script>

In my opinion, a better solution would be detecting the current page with Request::segment() (or Request::is() , depending on the stucture of your URLs) method. 我认为,更好的解决方案是使用Request::segment() (或Request::is() ,具体取决于您的URL的结构)方法检测当前页面。 Combine that with a View composer to avoid passing data to each view manually. 将其与View Composer结合使用可避免将数据手动传递给每个视图。

An example: 一个例子:

The view composer: 视图编辑器:

class PageComposer
{
    public function compose($view)
    {
        $view->with('active', Request::segment(1));
    }
}

The service provider: 服务提供者:

class PageServiceProvider extends ServiceProvider
{
    public function register()
    {
        View::composer('partials.header', 'PageComposer');
    }
}

The part of the header file that is common to all your pages: 头文件中所有页面共有的部分:

<ul id='pages'>
    <li id='page1'><a href="page1">page1</a></li>
    <li id='page2'><a href="page2">page2</a></li>
    <li id='page3'><a href="page3">page3</a></li>
</ul>

The script that changes the active class: 更改活动类的脚本:

<script>
    $(document).ready(function () {
       var activeLink = $('#{{ $active }}');               
       activeLink.addClass('active');
    });
</script>

More information about this in Laravel 4 documentation: Laravel 4文档中有关此的更多信息:

Requests 要求

View Composers 查看作曲家

You can compare anchor href to pathname and then add style or assign class to correct anchor eg 您可以比较锚href和路径名,然后添加样式或分配类以更正锚,例如

...

<li><a href="index.php">Link</a></li>

...

$('a[href="'+window.location.pathname.split('/').pop()+'"]').css({ color: 'red' });

or 要么

$('a[href="'+window.location.pathname.split('/').pop()+'"]').addClass('current');

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

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