2016-07-06 15 views
0

AJAXタブを動作させようとしています。現在のコードの問題は、アクティブなタブのスタイルを設定していないことです。アクティブなタブのみの背景色を設定する方法

私はいくつかのJavaScriptコードでこれに取り組む必要があるかもしれませんが、実際にどこから始めるべきかはわかりません。私は、アクティブなタブの残りの部分から目立つようにはどうすればよい

HTML

$("h2").addClass("white"); 
 
    $(document).ready(function() { 
 
     // load index page when the page loads 
 
     $("#response").html("DEFAULT"); 
 
     $("#page1").click(function() { 
 
      // load home page on click 
 
      $("#response").html("<h2> Welcome </h2><p>Sed porttitor lectus nibh. Cras ultricies ligula sed magna dictum porta. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. <br/> Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Nulla quis lorem ut libero malesuada feugiat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>"); 
 
     }); 
 
     $("#page2").click(function() { 
 
      // load about page on click 
 
      $("#response").html("TESTING 2"); 
 
     }); 
 
     $("#page3").click(function() { 
 
      // load contact form onclick 
 
      $("#response").html("TESTING 3"); 
 
     }); 
 
     $("#page4").click(function() { 
 
      // load contact form onclick 
 
      $("#response").html("TESTING 4"); 
 
     }); 
 
     $("#page5").click(function() { 
 
      // load contact form onclick 
 
      $("#response").html("TESTING 5"); 
 
     }); 
 
    });
li { 
 
    display: inline-block; 
 
    margin-right: 2px; 
 

 
} 
 
li a { 
 
    
 
    padding: 4px; 
 
    color:#0086ca; 
 
    text-decoration: none; 
 
} 
 

 
.white { 
 
    color: green; 
 
} 
 

 
.contentarea { 
 
    margin-top: 30px; 
 
    width: 100%; 
 
    padding: 40px 50px; 
 
    background-color: #0086ca; 
 
    color: white; 
 
    text-align: left; 
 
    font-size: 24px; 
 
} 
 

 
/* -------------------------------- 
 

 
Multi steps indicator 
 

 
-------------------------------- */ 
 
@media only screen and (min-width: 768px) { 
 
    .cd-multi-steps { 
 
    /* reset style */ 
 
    background-color: transparent; 
 
    padding: 0; 
 
    text-align: center; 
 
    } 
 

 
    .cd-multi-steps li { 
 
    position: relative; 
 
    float: none; 
 
    margin: 0.4em 40px 0.4em 0; 
 
    } 
 
    .cd-multi-steps li:last-of-type { 
 
    margin-right: 0; 
 
    } 
 
    .cd-multi-steps li::after { 
 
    /* this is the line connecting 2 adjacent items */ 
 
    position: absolute; 
 
    content: ''; 
 
    height: 4px; 
 
    background: #edeff0; 
 
    /* reset style */ 
 
    margin: 0; 
 
    } 
 
    .cd-multi-steps li.visited::after { 
 
    background-color: #96c03d; 
 
    } 
 
    .cd-multi-steps li > *, .cd-multi-steps li.current > * { 
 
    position: relative; 
 
    color: #2c3f4c; 
 
    } 
 

 
    .cd-multi-steps.custom-separator li::after { 
 
    /* reset style */ 
 
    height: 4px; 
 
    background: #edeff0; 
 
    } 
 

 
    .cd-multi-steps.text-center li::after { 
 
    width: 100%; 
 
    top: 50%; 
 
    left: 100%; 
 
    -webkit-transform: translateY(-50%) translateX(-1px); 
 
    -moz-transform: translateY(-50%) translateX(-1px); 
 
    -ms-transform: translateY(-50%) translateX(-1px); 
 
    -o-transform: translateY(-50%) translateX(-1px); 
 
    transform: translateY(-50%) translateX(-1px); 
 
    } 
 
    .cd-multi-steps.text-center li > * { 
 
    z-index: 1; 
 
    padding: .6em 1em; 
 
    border-radius: .25em; 
 
    background-color: #edeff0; 
 
    } 
 
    .no-touch .cd-multi-steps.text-center a:hover { 
 
    background-color: #2c3f4c; 
 
    } 
 
    .cd-multi-steps.text-center li.current > *, .cd-multi-steps.text-center li.visited > * { 
 
    color: #ffffff; 
 
    background-color: #96c03d; 
 
    } 
 
    .cd-multi-steps.text-center.custom-icons li.visited a::before { 
 
    /* change the custom icon for the visited item - check icon */ 
 
    background-position: 0 -60px; 
 
    } 
 

 
    .cd-multi-steps.text-top li, .cd-multi-steps.text-bottom li { 
 
    width: 80px; 
 
    text-align: center; 
 
    } 
 
    .cd-multi-steps.text-top li::after, .cd-multi-steps.text-bottom li::after { 
 
    /* this is the line connecting 2 adjacent items */ 
 
    position: absolute; 
 
    left: 50%; 
 
    /* 40px is the <li> right margin value */ 
 
    width: calc(100% + 40px); 
 
    } 
 
    .cd-multi-steps.text-top li > *::before, .cd-multi-steps.text-bottom li > *::before { 
 
    /* this is the spot indicator */ 
 
    content: ''; 
 
    position: absolute; 
 
    z-index: 1; 
 
    left: 50%; 
 
    right: auto; 
 
    -webkit-transform: translateX(-50%); 
 
    -moz-transform: translateX(-50%); 
 
    -ms-transform: translateX(-50%); 
 
    -o-transform: translateX(-50%); 
 
    transform: translateX(-50%); 
 
    height: 12px; 
 
    width: 12px; 
 
    border-radius: 50%; 
 
    background-color: #edeff0; 
 
    } 
 
    .cd-multi-steps.text-top li.visited > *::before, 
 
    .cd-multi-steps.text-top li.current > *::before, .cd-multi-steps.text-bottom li.visited > *::before, 
 
    .cd-multi-steps.text-bottom li.current > *::before { 
 
    background-color: #96c03d; 
 
    } 
 
    .no-touch .cd-multi-steps.text-top a:hover, .no-touch .cd-multi-steps.text-bottom a:hover { 
 
    color: #96c03d; 
 
    } 
 
    .no-touch .cd-multi-steps.text-top a:hover::before, .no-touch .cd-multi-steps.text-bottom a:hover::before { 
 
    box-shadow: 0 0 0 3px rgba(150, 192, 61, 0.3); 
 
    } 
 

 
    .cd-multi-steps.text-top li::after { 
 
    /* this is the line connecting 2 adjacent items */ 
 
    bottom: 4px; 
 
    } 
 
    .cd-multi-steps.text-top li > * { 
 
    padding-bottom: 20px; 
 
    } 
 
    .cd-multi-steps.text-top li > *::before { 
 
    /* this is the spot indicator */ 
 
    bottom: 0; 
 
    } 
 

 
    .cd-multi-steps.text-bottom li::after { 
 
    /* this is the line connecting 2 adjacent items */ 
 
    top: 3px; 
 
    } 
 
    .cd-multi-steps.text-bottom li > * { 
 
    padding-top: 20px; 
 
    } 
 
    .cd-multi-steps.text-bottom li > *::before { 
 
    /* this is the spot indicator */ 
 
    top: 0; 
 
    } 
 
} 
 
/* -------------------------------- 
 

 
Add a counter to the multi-steps indicator 
 

 
-------------------------------- */ 
 
.cd-multi-steps.count li { 
 
    counter-increment: steps; 
 
} 
 

 
.cd-multi-steps.count li > *::before { 
 
    content: counter(steps) " - "; 
 
} 
 

 
@media only screen and (min-width: 768px) { 
 
    .cd-multi-steps.text-top.count li > *::before, 
 
    .cd-multi-steps.text-bottom.count li > *::before { 
 
    /* this is the spot indicator */ 
 
    content: counter(steps); 
 
    height: 26px; 
 
    width: 26px; 
 
    line-height: 26px; 
 
    font-size: 1.4rem; 
 
    color: #ffffff; 
 
    } 
 

 
    .cd-multi-steps.text-top.count li:not(.current) em::before, 
 
    .cd-multi-steps.text-bottom.count li:not(.current) em::before { 
 
    /* steps not visited yet - counter color */ 
 
    color: #2c3f4c; 
 
    } 
 

 
    .cd-multi-steps.text-top.count li::after { 
 
    bottom: 11px; 
 
    } 
 

 
    .cd-multi-steps.text-top.count li > * { 
 
    padding-bottom: 34px; 
 
    } 
 

 
    .cd-multi-steps.text-bottom.count li::after { 
 
    top: 11px; 
 
    } 
 

 
    .cd-multi-steps.text-bottom.count li > * { 
 
    padding-top: 34px; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<nav> 
 
    <ul class="cd-multi-steps text-bottom count"> 
 
     <li class="current"><a href="#tabs-1" id="page1" class="tab">page 1</a> 
 

 
     </li> 
 
     <li><a href="#tabs-2" id="page2" class="tab">page 2</a> 
 

 
     </li> 
 
     <li><a href="#tabs-3" id="page3" class="tab">page 3</a> 
 

 
     </li> 
 
     <li><a href="#tabs-4" id="page4" class="tab">page 4</a> 
 

 
     </li> 
 
     <li><a href="#tabs-5" id="page5" class="tab">page 5</a> 
 

 
     </li> 
 
    </ul> 
 
</nav> 
 
<div class="contentarea" id="response"></div>

ありがとうございました。

+0

これはhttp://codepen.io/nirmalyaghosh/pen/Goyqoaで動作しますか? –

+0

クラスを追加/削除することを学ぶhttp://api.jquery.com/category/manipulation/class-attribute/ – charlietfl

+0

下の編集された回答を参照してください。 – Miro

答えて

0

idをクリックする必要はありません。私はあなたのコードを簡略化し、クリック時に.activeを追加しました。それが役に立てば幸い。

 $("h2").addClass("white"); 
 
    $(document).ready(function() { 
 
     // load index page when the page loads 
 
     
 
     var data = { 
 
     '#tabs-1':'Testing 1', 
 
     '#tabs-2':'Testing 2', 
 
     '#tabs-3':'Testing 3', 
 
     '#tabs-4':'Testing 4', 
 
     '#tabs-5':'Testing 5' 
 
     }; 
 
     
 
     $("#response").html("DEFAULT"); 
 
     
 
     $('.tab').click(function(){ 
 
     $('.tab').removeClass('active'); //remove .active from all 
 
     $(this).addClass('active'); //add .active to current pressed 
 
     href = $(this).attr('href'); 
 
     $("#response").html(data[href]); 
 
     }); 
 

 
    });
li { 
 
    display: inline-block; 
 
    margin-right: 2px; 
 
} 
 
li a { 
 
    padding: 8px 4px; 
 
    color: #0086ca; 
 
    text-decoration: none; 
 
} 
 

 

 
li a.active { 
 
    color: white; 
 
    background:#0086ca; 
 
} 
 

 
.contentarea { 
 
    margin-top: 30px; 
 
    width: 100%; 
 
    padding: 40px 50px; 
 
    background-color: #0086ca; 
 
    color: white; 
 
    text-align: left; 
 
    font-size: 24px; 
 
} 
 
.cd-multi-steps.count li { 
 
    counter-increment: steps; 
 
} 
 
.cd-multi-steps.count li > *::before { 
 
    /*content: counter(steps)" - ";*/ 
 
} 
 
@media only screen and (min-width: 768px) { 
 
    .cd-multi-steps.text-top.count li > *::before, 
 
    .cd-multi-steps.text-bottom.count li > *::before { 
 
    /* this is the spot indicator 
 
    content: counter(steps);*/ 
 
    height: 26px; 
 
    width: 26px; 
 
    line-height: 26px; 
 
    font-size: 1.4rem; 
 
    color: #ffffff; 
 
    } 
 
    .cd-multi-steps.text-top.count li:not(.current) em::before, 
 
    .cd-multi-steps.text-bottom.count li:not(.current) em::before { 
 
    /* steps not visited yet - counter color */ 
 
    color: #2c3f4c; 
 
    } 
 
    .cd-multi-steps.text-top.count li::after { 
 
    bottom: 11px; 
 
    } 
 
    .cd-multi-steps.text-top.count li > * { 
 
    padding-bottom: 34px; 
 
    } 
 
    .cd-multi-steps.text-bottom.count li::after { 
 
    top: 11px; 
 
    } 
 
    .cd-multi-steps.text-bottom.count li > * { 
 
    padding-top: 34px; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<nav> 
 
    <ul class="cd-multi-steps text-bottom count"> 
 
    <li class="current"><a href="#tabs-1" id="page1" class="tab">page 1</a> 
 

 
    </li> 
 
    <li class="current"><a href="#tabs-2" id="page2" class="tab">page 2</a> 
 

 
    </li> 
 
    <li><a href="#tabs-3" id="page3" class="tab">page 3</a> 
 

 
    </li> 
 
    <li><a href="#tabs-4" id="page4" class="tab">page 4</a> 
 

 
    </li> 
 
    <li><a href="#tabs-5" id="page5" class="tab">page 5</a> 
 

 
    </li> 
 
    </ul> 
 
</nav> 
 
<div class="contentarea" id="response"></div>

0

チェックこのコードには、クリック機能の内部で

使用addClass()を助けるかもしれ

$("li").click(function(){ 
    $(this).addClass("active").siblings().removeClass('active'); 

    }) 
$("#page1").click(function() { 
// load home page on click 
$("#response").html("<h2> Welcome </h2><p>Sed porttitor lectus nibh. Cras ultricies ligula sed magna dictum porta. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. <br/> Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Nulla quis lorem ut libero malesuada feugiat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>"); 
$("h2").addClass("white"); 
     }); 

$(document).ready(function() { 
 
     // load index page when the page loads 
 
     $("#response").html("DEFAULT"); 
 
    
 
    $("li").click(function(){ 
 
    $(this).addClass("active").siblings().removeClass('active'); 
 
    
 
    }) 
 
     $("#page1").click(function() { 
 
      // load home page on click 
 
      $("#response").html("<h2> Welcome </h2><p>Sed porttitor lectus nibh. Cras ultricies ligula sed magna dictum porta. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. <br/> Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Nulla quis lorem ut libero malesuada feugiat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>"); 
 
      $("h2").addClass("white"); 
 
     }); 
 
     $("#page2").click(function() { 
 
      // load about page on click 
 
      $("#response").html("TESTING 2"); 
 
     }); 
 
     $("#page3").click(function() { 
 
      // load contact form onclick 
 
      $("#response").html("TESTING 3"); 
 
     }); 
 
     $("#page4").click(function() { 
 
      // load contact form onclick 
 
      $("#response").html("TESTING 4"); 
 
     }); 
 
     $("#page5").click(function() { 
 
      // load contact form onclick 
 
      $("#response").html("TESTING 5"); 
 
     }); 
 
     
 
    });
li { 
 
    display: inline-block; 
 
    margin-right: 2px; 
 
} 
 
li a { 
 
    padding: 4px; 
 
    color:#0086ca; 
 
    text-decoration: none; 
 
} 
 
.contentarea { 
 
    margin-top: 30px; 
 
    width: 100%; 
 
    padding: 40px 50px; 
 
    background-color: #0086ca; 
 
    color: white; 
 
    text-align: left; 
 
    font-size: 24px; 
 
} 
 
.cd-multi-steps.count li { 
 
    counter-increment: steps; 
 
} 
 
.cd-multi-steps.count li > *::before { 
 
    content: counter(steps) " - "; 
 
} 
 
@media only screen and (min-width: 768px) { 
 
    .cd-multi-steps.text-top.count li > *::before, 
 
    .cd-multi-steps.text-bottom.count li > *::before { 
 
    /* this is the spot indicator */ 
 
    content: counter(steps); 
 
    height: 26px; 
 
    width: 26px; 
 
    line-height: 26px; 
 
    font-size: 1.4rem; 
 
    color: #ffffff; 
 
    } 
 
.cd-multi-steps.text-top.count li:not(.current) em::before, 
 
    .cd-multi-steps.text-bottom.count li:not(.current) em::before { 
 
    /* steps not visited yet - counter color */ 
 
    color: #2c3f4c; 
 
    } 
 
.cd-multi-steps.text-top.count li::after { 
 
    bottom: 11px; 
 
    } 
 
.cd-multi-steps.text-top.count li > * { 
 
    padding-bottom: 34px; 
 
    } 
 
.cd-multi-steps.text-bottom.count li::after { 
 
    top: 11px; 
 
    } 
 
.cd-multi-steps.text-bottom.count li > * { 
 
    padding-top: 34px; 
 
    } 
 
} 
 
.white{color:yellow} 
 
li.active a{color:blue; border:1px solid;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<nav> 
 
    <ul class="cd-multi-steps text-bottom count"> 
 
     <li class="current"><a href="#tabs-1" id="page1" class="tab">page 1</a> 
 

 
     </li> 
 
     <li class="current"><a href="#tabs-2" id="page2" class="tab">page 2</a> 
 

 
     </li> 
 
     <li><a href="#tabs-3" id="page3" class="tab">page 3</a> 
 

 
     </li> 
 
     <li><a href="#tabs-4" id="page4" class="tab">page 4</a> 
 

 
     </li> 
 
     <li><a href="#tabs-5" id="page5" class="tab">page 5</a> 
 

 
     </li> 
 
    </ul> 
 
</nav> 
 
<div class="contentarea" id="response"></div>

関連する問題