Twitter bootstrap tabs and javascript events

后端 未结 7 1515
悲哀的现实
悲哀的现实 2021-01-30 05:15

I am using twitter bootstrap for a project - in particular its tab functions (http://twitter.github.com/bootstrap/javascript.html#tabs)

Now I have this tablist and when

相关标签:
7条回答
  • 2021-01-30 05:30

    How about using click? Like this:

    $('#.tabs').click(function (e) {
      e.target(window.alert("hello"))
    })
    
    0 讨论(0)
  • 2021-01-30 05:34

    Documentation says: use event show.

    0 讨论(0)
  • 2021-01-30 05:36

    You can use the following snippet of code in bootstrap 3

    $(document).on( 'shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
       var activatedTab = e.target; // activated tab
    })
    
    0 讨论(0)
  • 2021-01-30 05:40

    If you are using 2.3.2 and it's not working, try using:

    $(document).on("shown", 'a[data-toggle="tab"]', function (e) {
         console.log('showing tab ' + e.target); // Active Tab
         console.log('showing tab ' + e.relatedTarget); // Previous Tab
    });
    

    2.3.2 tabs usage: http://getbootstrap.com/2.3.2/javascript.html#tabs

    If you are playing with version 3 (currently RC2) then documentation shows

    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
         console.log('showing tab ' + e.target); // Active Tab
         console.log('showing tab ' + e.relatedTarget); // Previous Tab
     })
    

    RC2 Tabs Usage: http://getbootstrap.com/javascript/#tabs-usage

    0 讨论(0)
  • 2021-01-30 05:40

    You have an error in your syntax relating to the selector:

    $('#.tabs') // should be ...
    $('.tabs');
    

    The unordered list has the class called 'tabs' in your HTML, whereas you were originally trying to select an element with the id '.tabs'.

    And you'll have to take manticore's suggestion into account too. You can only use 'change' on form elements.

    $(document).ready(function() {
        $('.tabs').click(function(e) {
            e.preventDefault();
            alert('tab clicked!');
        });
    });
    
    0 讨论(0)
  • 2021-01-30 05:41

    The bind seems to run before the DOM is ready. In addition, your selector seems to be broken and you can bind change only on select elements. You have to work around by clicking and implementing some logic. Try

    $(function() {
        $('.tabs').bind('click', function (e) {
            e.target(window.alert("hello"))
        });
    });
    

    UPDATE

    After consulting with the documentation, it seems that your code was only missing the DOM ready part, the rest wasn't actually a bug, which means that according to the docs the following should work:

    $(function() {
        $('.tabs').bind('change', function (e) {
            // e.target is the new active tab according to docs
            // so save the reference in case it's needed later on
            window.activeTab = e.target;
            // display the alert
            alert("hello");
            // Load data etc
        });
    });
    

    What caused confusion is that the plugin overrides default selector, making #.tabs valid and also adding a change event to the tab element. Go figure why they decided this syntax was a great idea.

    Anyways you can try the second sample and comment wether the alert is fired before or after the tab change.

    EDIT: fixed jquery exception caused by #.tabs selector

    0 讨论(0)
提交回复
热议问题