Javascript 根据URL更改更改DOM元素类名

Javascript 根据URL更改更改DOM元素类名,javascript,jquery,html,Javascript,Jquery,Html,我有一段代码,可以获取URL中的任何更改 每当有人输入散列时,它就会工作并注意到 但是,我想更改容器的类名。当我尝试改变所有的类名时,我知道为什么 是否仍然需要指定索引,例如 $(“.agenda tabs li”)。此('class','active') 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <ti

我有一段代码,可以获取URL中的任何更改

每当有人输入散列时,它就会工作并注意到

但是,我想更改容器的类名。当我尝试改变所有的类名时,我知道为什么

是否仍然需要指定索引,例如
$(“.agenda tabs li”)。此('class','active')

代码如下:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>jQuery Example</title>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="https://raw.githubusercontent.com/cowboy/jquery-hashchange/v1.3/jquery.ba-hashchange.min.js"></script>

        <script type="text/javascript">

        $(document).ready(function(){

            $( "#seminars" ).hide();
            $( "#speakers" ).hide();

            $(function(){

                // Bind the event.
                $(window).hashchange( function(){
                    // Alerts every time the hash changes!
                    var currenthash = location.hash;

                    $('.agenda-tabs').find('.ui-link').each(function(){
                        var innerDivId = $(this).attr('href');

                        if(innerDivId==currenthash)
                        {
                            $(".agenda-tabs li").attr('class', 'active');
                        }
                        else
                        {
                            $(".agenda-tabs li").attr('class', '');
                        }
                    });

                })

                // Trigger the event (useful on page load).
                $(window).hashchange();
            });

        });
        </script>
    </head>
    <body>

        <ul class="agenda-tabs nav nav-tabs">
            <li class="first-tab">
                <a class="btn btn-default ui-link" data-toggle="tab" href="#t1">
                City
                <span></span>
                </a>
                <div id='conference'>
                1
                </div>
            </li>
            <li class="active">
                <a class="btn btn-default ui-link" data-toggle="tab" href="#t2">
                Speakers
                <span></span>
                </a>
                <div id='seminars'>
                2
                </div>
            </li>
            <li class="last-tab">
                <a class="btn btn-default ui-link" data-toggle="tab" href="#t3">
                Stage
                <span></span>
                </a>
                <div id='speakers'>
                3
                </div>
            </li>
        </ul>

    </body>
</html>

jQuery示例
$(文档).ready(函数(){
$(“#研讨会”).hide();
$(“#扬声器”).hide();
$(函数(){
//绑定事件。
$(窗口).hashchange(函数(){
//每次哈希更改时都会发出警报!
var currenthash=location.hash;
$('.agenda tabs')。查找('.ui链接')。每个(函数(){
var innerDivId=$(this.attr('href');
if(innerDivId==currenthash)
{
$(“.agenda tabs li”).attr('class','active');
}
其他的
{
$(“.agenda tabs li”).attr('类','');
}
});
})
//触发事件(在页面加载时有用)。
$(window.hashchange();
});
});
  • 1.
  • 2.
  • 3.

是否可以为您的LI元素提供ID?然后,您可以将代码更改为:

$(".agenda-tabs li#" + currenthash).addClass('active');

是否可以为LI元素提供一个ID?然后,您可以将代码更改为:

$(".agenda-tabs li#" + currenthash).addClass('active');

是否可以为LI元素提供一个ID?然后,您可以将代码更改为:

$(".agenda-tabs li#" + currenthash).addClass('active');

是否可以为LI元素提供一个ID?然后,您可以将代码更改为:

$(".agenda-tabs li#" + currenthash).addClass('active');

在上下文变量(
this
)中已经有一个锚点
元素。您只需更改父元素
  • // get the parent li and set it as active
    $(this).closest("li").addClass('active'); // Add
    


    在上下文变量(
    this
    )中已经有一个锚点
    元素。您只需更改父元素
  • // get the parent li and set it as active
    $(this).closest("li").addClass('active'); // Add
    


    在上下文变量(
    this
    )中已经有一个锚点
    元素。您只需更改父元素
  • // get the parent li and set it as active
    $(this).closest("li").addClass('active'); // Add
    


    在上下文变量(
    this
    )中已经有一个锚点
    元素。您只需更改父元素
  • // get the parent li and set it as active
    $(this).closest("li").addClass('active'); // Add
    


    您可以更改对此的绑定

    $(window).hashchange( function(){
    
      var currenthash = location.hash;
    $(".agenda-tabs li").removeClass('active')
    
    $('.agenda-tabs').find('.ui-link').each(function(){
        var innerDivId = $(this).attr('href');
    
        if(innerDivId==currenthash)
        {
           $(".agenda-tabs li").addClass('active');
    
        }
    
    });
    

    您可以更改对此的绑定

    $(window).hashchange( function(){
    
      var currenthash = location.hash;
    $(".agenda-tabs li").removeClass('active')
    
    $('.agenda-tabs').find('.ui-link').each(function(){
        var innerDivId = $(this).attr('href');
    
        if(innerDivId==currenthash)
        {
           $(".agenda-tabs li").addClass('active');
    
        }
    
    });
    

    您可以更改对此的绑定

    $(window).hashchange( function(){
    
      var currenthash = location.hash;
    $(".agenda-tabs li").removeClass('active')
    
    $('.agenda-tabs').find('.ui-link').each(function(){
        var innerDivId = $(this).attr('href');
    
        if(innerDivId==currenthash)
        {
           $(".agenda-tabs li").addClass('active');
    
        }
    
    });
    

    您可以更改对此的绑定

    $(window).hashchange( function(){
    
      var currenthash = location.hash;
    $(".agenda-tabs li").removeClass('active')
    
    $('.agenda-tabs').find('.ui-link').each(function(){
        var innerDivId = $(this).attr('href');
    
        if(innerDivId==currenthash)
        {
           $(".agenda-tabs li").addClass('active');
    
        }
    
    });
    

    我刚刚更新了addClass和removeClass方法。它不会更改刚刚更新为addClass和removeClass方法的
  • elementI上的任何其他类。它不会更改刚刚更新为addClass和removeClass方法的
  • elementI上的任何其他类。它不会更改刚刚更新为addClass和removeClass方法的
  • elementI上的任何其他类。它不会更改
  • 元素上的任何其他类