Jquery event fire with bootstrap tab

偶尔善良 提交于 2019-12-12 03:53:11

问题


I am using bootstrap tab in my view

    @Html.ValidationSummary(true)
        <ul id="MyTabs" class="nav nav-tabs nav-justified">
            <li class="active"><a data-toggle="tab" href="#details" id="TabDetailsLink">Personal
                Details</a></li>
            <li><a data-toggle="tab" href="#tab1" id="Tab1">T1</a></li>
            <li><a data-toggle="tab" href="#tab2" id="Tab2">T2</a></li>

        </ul>

and i use jquery for catch the event
$(".MyTabs li").click(function (e) {
    alert(2);
});  

My expectation is to get the alert whenever I change the tabs. But the event is not getting fired. Mine is an ASP.NET MVC 4 web api application.


回答1:


Your jQuery selector is wrong ! MyTabs is the Id. So use it with # prefix

This should work.

$("#MyTabs li").click(function (e) {
    e.prevelitDefault(); 
    alert(2);
});

Or you can use your existing jQuery selector if you add this class to yuur ul element.

<ul id="MyTabs" class="MyTabs nav nav-tabs nav-justified">

When using Id as your jQuery selector, use # prefix ( Ex : $("#myElementUniqueID"))

When using css class as your jQuery selector, use . prefix ( Ex : $(".myCssClss") )



来源:https://stackoverflow.com/questions/35510425/jquery-event-fire-with-bootstrap-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!