Twitter bootstrap tabs and javascript events

后端 未结 7 1513
悲哀的现实
悲哀的现实 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: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

提交回复
热议问题