Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 当URL包含特定字符串时,如何向元素添加/删除类?_Jquery - Fatal编程技术网

Jquery 当URL包含特定字符串时,如何向元素添加/删除类?

Jquery 当URL包含特定字符串时,如何向元素添加/删除类?,jquery,Jquery,我有一些标签: <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li role="presentation" id="hometab" class="active"><a href="http://www.mypage.com/" >Home</a></li> <li role="presentation" id="conten

我有一些标签:

<!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist">
    <li role="presentation" id="hometab" class="active"><a href="http://www.mypage.com/" >Home</a></li>
    <li role="presentation" id="contenttab"><a href="#content" aria-controls="content" role="tab" data-toggle="tab">content</a></li>
  </ul>

  <!-- Tab panes -->
  <div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="home">home</div>
    <div role="tabpanel" class="tab-pane" id="content">content</div>
  </div>

我的脚本不起作用
#contenttab
始终处于活动状态,
#hometab
从不处于活动状态。

尝试检查以下内容:

if ( url.indexOf( '?dir' ) !== -1 ) { ... }
或者尝试:

var url = location.search; // or location.href;

if ( url.indexOf( '?dir' ) !== -1 ) { ... }

请尝试使用以下选项进行检查:

if ( url.indexOf( '?dir' ) !== -1 ) { ... }
或者尝试:

var url = location.search; // or location.href;

if ( url.indexOf( '?dir' ) !== -1 ) { ... }

尝试使用测试:

<script type="text/javascript">
        $(document).ready(function() {
            if (/\?dir/.test(location.pathname)){
                $("#contenttab").attr("class","active");
                $("#hometab").removeClass("active");
            } else {
                $("#contenttab").removeClass("active");
                $("#hometab").attr("class","active");
            }
        });        
</script>
注意:如果参数多次出现 (?dir=lorem&dir=ipsum),您将获得第一个值(lorem)


尝试使用测试:

<script type="text/javascript">
        $(document).ready(function() {
            if (/\?dir/.test(location.pathname)){
                $("#contenttab").attr("class","active");
                $("#hometab").removeClass("active");
            } else {
                $("#contenttab").removeClass("active");
                $("#hometab").attr("class","active");
            }
        });        
</script>
注意:如果参数多次出现 (?dir=lorem&dir=ipsum),您将获得第一个值(lorem)


我试过了,现在hometab总是处于活动状态,
location.pathname
的值是多少?是的,`location.search`有效!猜对了,因为通常是
位置。路径名
只返回
/
之前的
?dir
。我试过了,现在#hometab总是处于活动状态
位置的值是多少。路径名
?是的,`location.search`有效!猜对了,因为通常是
位置。路径名
只返回
/
之前的
?dir
。谢谢,但是现在如果URL是
http://www.mypage/?dir=something
主页选项卡处于活动状态。谢谢,但是现在如果URL是
http://www.mypage/?dir=something
主页选项卡处于活动状态。如果URL为
http://www.mypage/?dir=something
主页选项卡处于活动状态。这不是你想要的。知道吗,如果URL是
http://www.mypage/?dir=something
主页选项卡处于活动状态。这不是您想要的?我需要
内容选项卡处于活动状态