简体   繁体   中英

Bootstrap Nav Tabs - Fire Javascript

I'm using a basic Bootstrap tab structure, where I wish to fire a Javascript event once the tab is clicked. I can't figure out how to do this. This is the code I have come up with so far:

            <ul class="nav nav-tabs nav-justified">
              <li class="active"><a data-toggle="tab" href="#tab1"> tab(<?php echo $count ?>) </a></li>
              <li><a data-toggle="tab" href="#menu2"> tab(<?php echo $count3 ?>) </a></li>
              <li><a data-toggle="tab" href="#menu1"> tab(<?php echo $count2 ?>) </a></li>                    
            </ul>

and the Javascript:

$("#menu2").click(function() {
        alert('yes, the click actually happened');
    });
$('.nav-tabs a[href="#menu2"]').click(function() {

You have to do:

 $('[href=#menu2]').on('shown.bs.tab', function (e) {
             alert('Your Code')
 });

This event fires on tab show after a tab has been shown. Use event.target and event.relatedTarget to target the active tab and the previous active tab (if available) respectively.

http://getbootstrap.com/javascript/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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