2017-06-09 1 views
0

クリックしたリンクに基づいてコンテンツを切り替えるjQueryの表示/非表示を使用してコードを設定しました。コンテンツを切り替える3つのリンクは、1つのチャプター、2つのチャプター、3つのチャプターです。私が解決しようとしていることの1つは、コンテンツが表示されている間、リンクの色をアクティブにすることです。 3つのリンクの1つがクリックされるたびに、色が変わります。可能であれば、どうすれば私の現在のコードでこれを行うのですか?このトグル・コンテンツを行うためのより良い方法があれば、私はそれにもオープンしています。 私はここfiddlejQueryのナビゲーションリンクのアクティブリンクの色

HTML 

    <table id="menu" style="margin: 0px; background-color:transparent; border: 0px solid ##000; width: 65%;"> 
        <tr> 
        <td style="padding: 1px 2px 1px 0px; font-size:12px;"><a data-chart="chart1" href="##">CHART ONE</a></td> 
        <td style="padding: 1px 8px; 1px 0px; font-size:12px;"><a data-chart="chart2" href="##">CHART TWO</td> 
        <td style="padding: 1px 2px; 1px 0px; font-size:12px;"><a data-chart="chart3" href="##">CHART THREE</td> 
        </tr> 
        </td> 
        </table> 

        <div id="charts"> 
    <div id="chart1" class="chart" data-chart="chart1"> 
     <table class="tbl" style="background-color: ##fff;"> 

        <tr> 
         <th>Chart One</th> 
         <th>Header</th> 
         <th>Header</th> 
         <th>Header</th> 
        </tr> 
        <tr style="background-color: ##959595;"> 
         <td style="background-color: ##4e4f4f;">Text</td> 
         <td class="x">&nbsp;</td> 
         <td class="x">&nbsp;</td> 
         <td class="x">Text</td> 
        </tr> 
        <tr style="background-color: ##aaa;"> 
         <td style="background-color: ##767777;">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
        </tr> 
        <tr style="background-color: ##959595;"> 
         <td style="background-color: ##4e4f4f;">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
     </table> 
    </div> 
    <div id="chart2" class="chart hide" data-chart="chart2"> 
     <table class="tbl" style="background-color: ##fff;"> 

        <tr> 
         <th>Chart Two</th> 
         <th>Header</th> 
         <th>Header</th> 
         <th>Header</th> 
        </tr> 
        <tr style="background-color: ##959595;"> 
         <td style="background-color: ##4e4f4f;">Text</td> 
         <td class="x">&nbsp;</td> 
         <td class="x">&nbsp;</td> 
         <td class="x">Text</td> 
        </tr> 
        <tr style="background-color: ##aaa;"> 
         <td style="background-color: ##767777;">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
        </tr> 
        <tr style="background-color: ##959595;"> 
         <td style="background-color: ##4e4f4f;">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
     </table> 
    </div> 
    <div id="chart3" class="chart hide" data-chart="chart3"> 
     <table class="tbl" style="background-color: ##fff;"> 

        <tr> 
         <th>Chart Three</th> 
         <th>Header</th> 
         <th>Header</th> 
         <th>Header</th> 
        </tr> 
        <tr style="background-color: ##959595;"> 
         <td style="background-color: ##4e4f4f;">Text</td> 
         <td class="x">&nbsp;</td> 
         <td class="x">&nbsp;</td> 
         <td class="x">Text</td> 
        </tr> 
        <tr style="background-color: ##aaa;"> 
         <td style="background-color: ##767777;">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
        </tr> 
        <tr style="background-color: ##959595;"> 
         <td style="background-color: ##4e4f4f;">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
         <td class="x">Text</td> 
     </table> 
CSS 

    ##menu a:link { 
    color: #fff; 
    border-bottom: none; 
    text-decoration: none; 
    font-weight: 600; 
    padding: 2px 0px; 
} 

##menu a:visited { 
    color: #fff; 
    border-bottom: none; 
    text-decoration: none; 
    font-weight: 600; 
    padding: 2px 0px; 
} 

##menu a:hover { 
    color: #fff; 
    border-bottom: none; 
    text-decoration: none; 
    font-weight: 600; 
    padding: 2px 0px; 
} 

##menu a:active { 
    color: #000; 
    border-bottom: none; 
    text-decoration: none; 
    font-weight: 600; 
    padding: 2px 0px; 
} 

Javascript 

    $(document).ready(function() { 
    $("#menu a").on('click', function(e) { 
     e.preventDefault() 
     var chart = $(this).data('chart'); 
     $("#charts .chart:not('.hide')").stop().fadeOut('fast', function() { 
      $(this).addClass('hide'); 
      $('#charts .chart[data-chart="'+chart+'"]').fadeIn('slow').removeClass('hide'); 
     }); 
    }); 
}); 

おかげでフィドルを作成しました!

答えて

0

リンクをクリックすると、アクティブであることを示すクラスを追加し、他のリンクからアクティブなクラスを削除します。あなたのCSSに

$(document).ready(function() { 
 
    var $links = $('#menu a'); 
 
    $links.on('click', function(e) { 
 
    $links.not($(this)).removeClass('active'); 
 
    $(this).addClass('active'); 
 
    e.preventDefault() 
 
    var chart = $(this).data('chart'); 
 
    $("#charts .chart:not('.hide')").stop().fadeOut('fast', function() { 
 
     $(this).addClass('hide'); 
 
     $('#charts .chart[data-chart="' + chart + '"]').fadeIn('slow').removeClass('hide'); 
 
    }); 
 
    }); 
 
});
##menu a:link { 
 
    color: #fff; 
 
    border-bottom: none; 
 
    text-decoration: none; 
 
    font-weight: 600; 
 
    padding: 2px 0px; 
 
} 
 

 
##menu a:visited { 
 
    color: #fff; 
 
    border-bottom: none; 
 
    text-decoration: none; 
 
    font-weight: 600; 
 
    padding: 2px 0px; 
 
} 
 

 
##menu a:hover { 
 
    color: #fff; 
 
    border-bottom: none; 
 
    text-decoration: none; 
 
    font-weight: 600; 
 
    padding: 2px 0px; 
 
} 
 

 
##menu a:active { 
 
    color: #000; 
 
    border-bottom: none; 
 
    text-decoration: none; 
 
    font-weight: 600; 
 
    padding: 2px 0px; 
 
} 
 

 
.active { 
 
    color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="menu" style="margin: 0px; background-color:transparent; border: 0px solid ##000; width: 65%;"> 
 
    <tr> 
 
    <td style="padding: 1px 2px 1px 0px; font-size:12px;"><a data-chart="chart1" href="##">CHART ONE</a></td> 
 
    <td style="padding: 1px 8px; 1px 0px; font-size:12px;"><a data-chart="chart2" href="##">CHART TWO</td> 
 
    \t \t \t <td style="padding: 1px 2px; 1px 0px; font-size:12px;"><a data-chart="chart3" href="##">CHART THREE</td> 
 
    \t \t \t \t </tr> \t 
 
    \t \t \t \t </td> 
 
    \t \t \t \t </table> 
 
    \t \t \t \t 
 
    \t \t \t \t <div id="charts"> 
 
    <div id="chart1" class="chart" data-chart="chart1"> 
 
     <table class="tbl" style="background-color: ##fff;"> 
 
    \t \t \t \t 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <th>Chart One</th> 
 
\t \t \t \t \t \t <th>Header</th> 
 
\t \t \t \t \t \t <th>Header</th> 
 
\t \t \t \t \t \t <th>Header</th> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr style="background-color: ##959595;"> 
 
\t \t \t \t \t \t <td style="background-color: ##4e4f4f;">Text</td> 
 
\t \t \t \t \t \t <td class="x">&nbsp;</td> 
 
\t \t \t \t \t \t <td class="x">&nbsp;</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr style="background-color: ##aaa;"> 
 
\t \t \t \t \t \t <td style="background-color: ##767777;">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr style="background-color: ##959595;"> 
 
\t \t \t \t \t \t <td style="background-color: ##4e4f4f;">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
     </table> 
 
    </div> 
 
    <div id="chart2" class="chart hide" data-chart="chart2"> 
 
     <table class="tbl" style="background-color: ##fff;"> 
 
    \t \t \t \t 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <th>Chart Two</th> 
 
\t \t \t \t \t \t <th>Header</th> 
 
\t \t \t \t \t \t <th>Header</th> 
 
\t \t \t \t \t \t <th>Header</th> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr style="background-color: ##959595;"> 
 
\t \t \t \t \t \t <td style="background-color: ##4e4f4f;">Text</td> 
 
\t \t \t \t \t \t <td class="x">&nbsp;</td> 
 
\t \t \t \t \t \t <td class="x">&nbsp;</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr style="background-color: ##aaa;"> 
 
\t \t \t \t \t \t <td style="background-color: ##767777;">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr style="background-color: ##959595;"> 
 
\t \t \t \t \t \t <td style="background-color: ##4e4f4f;">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
     </table> 
 
    </div> 
 
    <div id="chart3" class="chart hide" data-chart="chart3"> 
 
     <table class="tbl" style="background-color: ##fff;"> 
 
    \t \t \t \t 
 
\t \t \t \t \t <tr> 
 
\t \t \t \t \t \t <th>Chart Three</th> 
 
\t \t \t \t \t \t <th>Header</th> 
 
\t \t \t \t \t \t <th>Header</th> 
 
\t \t \t \t \t \t <th>Header</th> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr style="background-color: ##959595;"> 
 
\t \t \t \t \t \t <td style="background-color: ##4e4f4f;">Text</td> 
 
\t \t \t \t \t \t <td class="x">&nbsp;</td> 
 
\t \t \t \t \t \t <td class="x">&nbsp;</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr style="background-color: ##aaa;"> 
 
\t \t \t \t \t \t <td style="background-color: ##767777;">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t </tr> 
 
\t \t \t \t \t <tr style="background-color: ##959595;"> 
 
\t \t \t \t \t \t <td style="background-color: ##4e4f4f;">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
\t \t \t \t \t \t <td class="x">Text</td> 
 
     </table>

0

$("#menu .active").removeClass("active"); 
$(this).addClass("active"); 

何ジャバスクリプトを追加するには、JavaScriptのメソッドに

#menu .active { 
    color: #ff0000; 
} 

その後、色を設定するために使用されるクラスを追加色を含むCSSスタイルを追加したり削除したりしますあなたがあなたのリンクのためにしたいtはそれがなぜあなたはちょうどブートストラップタブを使用していない

0

codepen

選択されるものである一方で?

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

    <div class="container"> 
     <ul class="nav nav-tabs"> 
     <li class="active"><a data-toggle="tab" href="#home">Home</a></li> 
    <li><a data-toggle="tab" href="#chart1">chart 1</a></li> 
    <li><a data-toggle="tab" href="#chart2">chart 2</a></li> 
    <li><a data-toggle="tab" href="#chart3">chart 3</a></li> 
    </ul> 

    <div class="tab-content"> 
    <div id="home" class="tab-pane fade in active"> 
     <h1>HOME</h1> 
    </div> 
    <div id="chart1" class="tab-pane fade"> 
     <h1>chart 1</h1> 

    </div> 
    <div id="chart2" class="tab-pane fade"> 
     <h1>chart 2</h1> 

    </div> 
    <div id="chart3" class="tab-pane fade"> 
     <h1>chart 3</h1> 

    </div> 
    </div> 
</div> 
+0

こんにちはザカリヤ、私はあなたのブートストラップの提案を試してみましたが、残念ながら私たちが使用している他のいくつかのスタイルとスクリプトコードを妨害BootstratpスタイルとJavaScriptで引っ張っ。それは素晴らしいアイデアのように見え、それを使うのが好きです。私は横にそれを実験し続けるつもりです。ありがとう! –

関連する問題