简体   繁体   English

<a>使用Javascript</a>将<a>类</a>设置<a>为“活动”</a>

[英]Set <a>class = “active” using Javascript

I have a nav bar that I've made common throughout pages so it's easier to update, however now my class="active" on the <a> tags is of course not working. 我有一个导航栏,该导航栏在整个页面上都很常见,因此更容易更新,但是现在<a>标记上的class="active"当然不起作用。 I've been trying to fiddle around with this using Javascript, so that when these are clicked, the active state adds to that link and is removed from the previous. 我一直在尝试使用Javascript来摆弄它,以便在单击它们时将活动状态添加到该链接中,并从前一个链接中删除。

 $(document).ready(function() { //active state $(function() { $('.start').addClass('active'); $('#Side_Menu a').click(function() { $('#Side_Menu a').removeClass('active'); $(this).addClass('active'); }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="nav nav-pills nav-stacked" id="Side_Menu"> <li><a href="dashboard.php" class="active"><i class="fa fa-th" aria-hidden="true"></i> &nbsp;Dashboard</a></li> <li><a href="blog_posts.php"><i class="fa fa-file-text" aria-hidden="true"></i> &nbsp;Blog Posts</a></li> <li><a href="addnewpost.php"><i class="fa fa-code" aria-hidden="true"></i> &nbsp;Add New Post</a></li> <li><a href="categories.php"><i class="fa fa-list" aria-hidden="true"></i> &nbsp;Categories</a></li> <li><a href="admins.php"><i class="fa fa-user" aria-hidden="true"></i> &nbsp;Manage Admins</a></li> </ul> 

I have the active state on the first <a> because this is the default load page. 我在第一个<a>active状态,因为这是默认的加载页面。 Not sure why this is not working. 不知道为什么这不起作用。 It navigates around & I'm not getting any errors, but the JS is not doing it's job. 它四处导航并且我没有收到任何错误,但是JS并没有完成它的工作。

Any help would be appreciated. 任何帮助,将不胜感激。

When you navigate to a different page, the whole thing starts again. 当您导航到另一个页面时,整个事情再次开始。 Your JS works when you click the link, but then straight away you navigate to the other page and everything starts from scratch. 当您单击链接时,您的JS正常运行,但是随后您立即导航到另一页,一切从头开始。

Just for fun, if you take away all the href attributes from the menu, your JS will work, but you won't navigate anywhere. 只是为了好玩,如果您从菜单中href了所有href属性,那么您的JS就可以使用,但是您无法在任何地方导航。

It's simplest to add the active class in your PHP templates instead of using JS. 在PHP模板中添加active类而不是使用JS是最简单的。 For example: 例如:

class="<?= $_SERVER['REQUEST_URI'] == 'dashboard.php' ? 'active' : '' ?>"...

Try this: 尝试这个:

    $(document).ready(function() {
        $(function() {
            $('.start').addClass('active');
            $('#Side_Menu a').click(function(e) {
                e.preventDefault();
                $('#Side_Menu a').removeClass('active');
                $(this).addClass('active');
            });
        });
    });

I prevented the default, just for testing purposes. 我仅出于测试目的而阻止了默认设置。 If the page is unloaded (you navigate to another page) you probably wont notice the changes on th HTML. 如果页面被卸载(您导航到另一个页面),您可能不会注意到HTML上的更改。

 $(document).ready(function() { $('.start').addClass('active'); $('#Side_Menu a').click(function(e) { e.preventDefault(); $('#Side_Menu a').removeClass('active'); $(this).addClass('active'); }); }); 
 a.active { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="nav nav-pills nav-stacked" id="Side_Menu"> <li><a href="dashboard.php" class="active"><i class="fa fa-th" aria-hidden="true"></i> &nbsp;Dashboard</a></li> <li><a href="blog_posts.php"><i class="fa fa-file-text" aria-hidden="true"></i> &nbsp;Blog Posts</a></li> <li><a href="addnewpost.php"><i class="fa fa-code" aria-hidden="true"></i> &nbsp;Add New Post</a></li> <li><a href="categories.php"><i class="fa fa-list" aria-hidden="true"></i> &nbsp;Categories</a></li> <li><a href="admins.php"><i class="fa fa-user" aria-hidden="true"></i> &nbsp;Manage Admins</a></li> </ul> 

EDIT: This code will select the current link in a very simple way using jQuery and parsing location.href : 编辑:这段代码将使用jQuery和解析location.href以非常简单的方式选择当前链接:

<script>
    $(document).ready(function() {
        var url      = String(location.href),
            selected = url.substr(url.lastIndexOf('/') + 1);
        $('#Side_Menu a[href="' + selected + '"]').addClass('active');
    });
</script>

You are repeating the same code (document ready): 您正在重复相同的代码(准备好文档):

$(document).ready(function(){
    //active state  
    $(function() {
        $('.start').addClass('active');
        $('#Side_Menu a').click(function() {
        $('#Side_Menu a').removeClass('active');
        $(this).addClass('active');             
        });
    });
});

You should and try to use this: 您应该尝试使用此方法:

$(document).ready(function(){
        //active state  
        $('.start').addClass('active');
        $('#Side_Menu li a').click(function() {
        $('#Side_Menu li a').removeClass('active');
        $(this).addClass('active'); 
        });            
});

And what's the "start" class for? 那“开始”课是干什么的呢?

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

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