0

嗨,我有以下代码允许用户在 3 个选项卡之间切换,它通过淡入和淡出来显示和隐藏 div,我希望这样,如果在导航栏上选择了当前选项卡,它不会淡出和淡入再次,相反,我希望它什么也不做。

继承人的代码:

HTML:

<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="220px">
<p align="left"><table width="220px" cellspacing="0" cellpadding="0">

<tr>
<td class="mya_nav" style="border-color: #B23387;">
<a id="show_personal">Personal</a>
</td>
</tr>

<tr>
<td class="mya_nav" style="border-color: #00c6ff;">
<a id="show_favourites">Favourites</a>
</td>
</tr>

<tr>
<td class="mya_nav" style="border-color: #00e60b;">
<a id="show_history">History</a>
</td>
</tr>

</table>
</p>

</td>
<td width="675px">

<div id="content">


    <div id="personal" <?php echo $personal_current; ?>>
        <p align="center">Personal</p>

    </div>
    <div id="favourites" <?php echo $favourite_current; ?>>
        <p align="center">Favourites</p>

    </div>
    <div id="history" <?php echo $recent_current; ?>>
        <p align="center">History</p>

    </div>
</div>
</td>
</tr>
</table>

JS:

<script>

    $('p a').click(function(){

        var id = $(this).html().toLowerCase();

        $('.current').fadeOut(600, function(){

            $('#'+id).fadeIn(600);
            $('.current').removeClass('current');
            $('#'+id).addClass('current');

        });

    });

</script>
4

3 回答 3

2
$('p a').click(function(){
    var id = $(this).html().toLowerCase();
    if (this.not(':focus')) {
        $('.current').fadeOut(600, function(){
            $('#'+id).fadeIn(600);
            $('.current').removeClass('current');
            $('#'+id).addClass('current');
    });
});
于 2013-03-08T06:49:10.637 回答
1

你可以试试

<script>

    $('p a').click(function(){

        var id = $(this).html().toLowerCase();
        if($('.current').attr('id')!=id){
            $('.current').fadeOut(600, function(){
                $('#'+id).fadeIn(600);
                $('.current').removeClass('current');
                 $('#'+id).addClass('current');

            });
        }

    });

</script>
于 2013-03-08T06:48:33.860 回答
0

如果.current指的是当前显示的选项卡,您应该在单击它时忽略它,通过检查单击的项目是否是当前项目。 如果项目类包含当前, jQuery 的 hasClass将返回 true,否则返回 false。

if(!$('#'+id).hasClass('current')){
    $('.current').fadeOut(600, function(){

        $('#'+id).fadeIn(600);
        $('.current').removeClass('current');
        $('#'+id).addClass('current');

    });
}
于 2013-03-08T07:07:59.770 回答