0

我需要在LIST ITEM click 上调用 js 函数,但是当事件触发时,它会调用事件的次数

JS

$('.first').click(function () {

    //remove previous class and add it to clicked tab
    items.removeClass('current');
    $(this).addClass('current');

    $('#v-nav>div.tab-content').hide().eq(items.index($(this))).show();

    window.location.hash = $(this).attr('tab');
});

HTML

<div id="v-nav">
                <ul>

                    <li tab="tab1" class=" first CmclTabCss  current "> <span class="spn_InerCom"> Commercial Sector </span> <span class="tab1Current spn_Nmbr"> </span></li>
                    <li tab="tab2" class="first DMTabCss"><span class="spn_InerDip">Diplomatic Missions</span><span class="tab1CurrentD spn_Nmbr"></span></li>
                    <li tab="tab3" class="first GovTabCss"><span class="spn_InerGov">Government Sector</span><span class="tab1CurrentG spn_Nmbr"></span></li>
                    <li tab="tab4" class="first WrldTabCss"><span class="spn_InerWld">Outside Kingdom</span><span class="tab1CurrentW spn_Nmbr"> </span></li>
                    <li tab="tab5" class="first ImgTabCss"><span class="spn_InerImg">Images</span><span class="tab1CurrentI spn_Nmbr"> </span></li>
                    <li tab="tab6" class="first"><span class="spn_InerNews">News Archive</span><span class="tab1CurrentN spn_Nmbr"> </span></li>

                    <li tab="tab7" class="first PplTabCss"><span class="spn_InerIndual">Site Users</span><span class="tab1CurrentIndual spn_Nmbr"></span></li>

                </ul>
</div>
4

1 回答 1

1

您提供的代码是正确的。在这里查看:jsfiddle

您的代码中必须有其他部分click多次触发处理程序。

对于描述的问题,此代码可以完成工作(为清楚起见,省略了您的特定代码):

$('.first').click(function () {
    $('.first').removeClass('current');
    $(this).addClass('current');
    window.location.hash = $(this).attr('tab');
});
于 2013-05-19T09:42:09.700 回答