简体   繁体   English

链接到引导程序选项卡,而无需修改“ a”标签

[英]Link to bootstrap tab with no modification to “a” tag

There have been multiple solutions posted here for linking to tabs when using the bootstrap framework, but I'm looking for a slightly different solution and wondering if its possible. 这里发布了多种解决方案,用于在使用引导框架时链接到选项卡,但是我正在寻找稍微不同的解决方案,并想知道它是否可能。

There's a jsfiddle solution here demonstrating the behaviour I require: http://jsfiddle.net/technotarek/3hJ46/ 这里有一个jsfiddle解决方案,演示了我需要的行为: http : //jsfiddle.net/technotarek/3hJ46/

Outline code below: 大纲代码如下:

js JS

$("a").on('click', function() {
    # check if link id matches an element with data-toggle="tab"?
    # activate tab?
});

html HTML

<div class="container">
    <p><a "tab-2">Go to Tab #2</a></p>

    <div class="row">
        <div class="span12">
            <ul class="nav nav-tabs" id="myTabs">
              <li class="active"><a id="tab-1" href="#one" data-toggle="tab">TAB #1</a></li>
              <li><a href="#two" id="tab-2" data-toggle="tab">TAB #2</a></li>
              <li><a href="#three" id="tab-3" data-toggle="tab">TAB #3</a></li>
            </ul>

            <div class="tab-content">
              <div class="tab-pane active" id="one">
                  <p>You're in Tab #1</p>
                  <p><a "tab-2">Go to Tab #2</a></p>
                  <p><a "tab-3">Go to Tab #3</a></p>
                </div>
              <div class="tab-pane" id="two">
                  <p>You're in Tab #2</p> 
                </div>
              <div class="tab-pane" id="three">
                  <p>Now #3.</p>
                  <p><a "tab-1">Back to Tab #1</a></p>
                </div>
            </div>
        </div>
    </div>
</div>

You can click a link within the tab, or from outside the tab (on the same page) and the tab becomes activated. 您可以单击选项卡内的链接,也可以单击选项卡外部的链接(在同一页面上),然后该选项卡将被激活。 However, this requires that "a" tags have attribute "data-tab-destination" with the tab name. 但是,这要求“ a”标签的标签名称具有属性“ data-tab-destination”。 Due to the way we're automatically generating the html it's currently impossible (or v. difficult) to know if the link will point to a tab or any other part of the webpage, so it is not possible to add this attribute. 由于我们自动生成html的方式,目前尚无法(或很难)知道链接是否指向网页的标签或其他任何部分,因此无法添加此属性。

I'm therefore hoping for a solution (using javascript) that will detect for all links whether it points to a tab by searching for any element with matching id with attribute data-toggle="tab". 因此,我希望找到一种解决方案(使用javascript),方法是通过搜索具有ID匹配且属性为data-toggle =“ tab”的任何元素来检测所有链接是否指向选项卡。

Any help greatly appreciated. 任何帮助,不胜感激。

HTML HTML

<div class="container">
    <div class="row">
        <div class="span12">
            <ul class="nav nav-tabs" id="myTabs">
              <li class="active"><a id="tab-1" href="#one" data-toggle="tab">TAB #1</a></li>
              <li><a href="#two" id="tab-2" data-toggle="tab">TAB #2</a></li>
              <li><a href="#three" id="tab-3" data-toggle="tab">TAB #3</a></li>
            </ul>

            <div class="tab-content">
              <div class="tab-pane active" id="one">
                  <p>You're in Tab #1</p>
                  <p><a href="#tab-2">Go to Tab #2</a></p>
                  <p><a href="#tab-3">Go to Tab #3</a></p>
                </div>
              <div class="tab-pane" id="two">
                  <p>You're in Tab #2</p> 
                </div>
              <div class="tab-pane" id="three">
                  <p>Now #3.</p>
                  <p><a href="#tab-1">Back to Tab #1</a></p>
                </div>
            </div>
        </div>
    </div>
</div>

JS JS

$("a").on('click', function() {
    var href = $(this).attr('href');
    /* Just a hack to get the ID. You may need to think this through
     * in more detail for your use case. */
    var id = href.replace("#", ""); 
    // http://api.jquery.com/multiple-attribute-selector
    if ($("a[data-toggle='tab'][id='" + id + "']").length) {
        $("#" + id).click();        
    }
});

JSFiddle 的jsfiddle

Edit 编辑

Noticed missing logic in my JS code. 注意到我的JS代码中缺少逻辑。

+ if ($("a[data-toggle='tab'][id='" + id + "']").length) {
- if ($("a[data-toggle='tab'][id='" + id + "']")) {

How can I detect if a selector returns null? 如何检测选择器是否返回null?

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

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