【问题标题】:Linking to a specific tab from a href link从 href 链接链接到特定选项卡
【发布时间】:2018-05-16 02:15:30
【问题描述】:

我正在尝试使用指向 player.html 的 href 链接链接 index.html --> Pele 选项卡。

这是我的 index.html 代码:

  <a href="players.html#client-panel-2"></a>

还有我的 players.html

              <ul data-tabs="data-tabs" id="example-tabs" class="tabs">
                <li class="tabs-title is-active" tab-community-amenities="tab-community-amenities"
                    role="presentation">
                  <div class="tabs-link" data="#client-panel-0" role="tab" aria-controls="client-panel-0"
                       aria-selected="false" id="client-panel-0-label">Messi
                  </div>
                </li>
                <li class="tabs-title" tab-apartment-amenities="tab-apartment-amenities" role="presentation">
                  <div class="tabs-link" data="#client-panel-1" role="tab" aria-controls="client-panel-1"
                       aria-selected="false" id="client-panel-1-label">Pele
                  </div>
                </li>
                <li class="tabs-title" tab-pet-policy="tab-pet-policy" role="presentation">
                  <div class="tabs-link" data="#client-panel-2" role="tab" aria-controls="client-panel-2"
                       aria-selected="false" id="client-panel-2-label">Kaka
                  </div>
                </li>
                <li class="tabs-title" tab-parking-details="tab-parking-details" role="presentation">
                  <div class="tabs-link" data="#client-panel-3" role="tab" aria-controls="client-panel-3"
                       aria-selected="false" id="client-panel-3-label">Benzema ;)
                  </div>
                </li>
                <li class="tabs-title" tab-parking-details="tab-parking-details" role="presentation">
                  <div class="tabs-link" data="#client-panel-4" role="tab" aria-controls="client-panel-4"
                       aria-selected="false" id="client-panel-4-label">Berbatov
                  </div>
                </li>
              </ul>

【问题讨论】:

    标签: javascript html hyperlink href


    【解决方案1】:

    使链接包含某种 url 变量

    <a href="players.html?selected=pele-id></a>
    

    使用 jquery 很好

    <script
      src="https://code.jquery.com/jquery-3.3.1.min.js"
      integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
      crossorigin="anonymous"></script>
    

    添加一个id标签,方便jquery查找

    <div id="pele-id" class="tabs-link" data="#client-panel-2" role="tab" aria-controls="client-panel-2"
                           aria-selected="false" id="client-panel-2-label">
    

    在脚本标签中获取该值。然后将 attr aria-selected 编辑为(我假设这是您尝试更改的内容)。

    <script>
    var url = new URL(window.location.href);
    var selector = url.searchParams.get("selected");
    $("#" + selector).attr("aria-selected", "true")
    </script>
    

    假设将 attr 更改为“true”会做你想做的事情,这应该适用于所有不同的

    【讨论】:

    • 你好,我已经有id="id="client-panel-2-label",我应该删除并使用id="pele-id"吗?还有哪个是正确的
    • 无论你想要什么,只要确保 jquery 中使用 $(".id") 的所有实例都是正确的 id。并且使用第二个是正确的,url参数不需要引号
    • 对不起,我才意识到是我在 mb 中加上引号
    猜你喜欢
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2017-08-21
    • 2015-07-12
    • 2015-06-18
    • 1970-01-01
    • 2020-10-04
    • 2012-05-07
    相关资源
    最近更新 更多