简体   繁体   English

在ul中选择href更改颜色

[英]on select a href change color in ul li

Here I use tab menu for select different tab i want to change the color of selected button, when I select button change it color 在这里我使用选项卡菜单选择其他选项卡,我想更改所选按钮的颜色,当我选择按钮时将其更改颜色

<div id="tabs">
    <ul>
    <li><a class="active" href="#" name="tab1">Tempu Services</a></li>
    <li><a href="#" name="tab2">Buy and Sale</a></li>
    <li><a href="#" name="tab2">Job Service</a></li>
    </ul>
</div>

and my css id and my JavaScript is 和我的CSS ID和我的JavaScript是

<script>
    $(window).load(function(){
    $("#tabs ul  li a").click(function () {
        $("#tabs ul li a").not(this).removeClass("active").addClass('unactive')
        $(this).toggleClass("unactive").toggleClass("active");
    });
});//]]>
</script>

i also use this script for this 我也为此使用此脚本

        <script>
$(document).ready(function() {
    $("#contenty").find("[id^='tab']").hide(); 
    $("#tabs li:first").attr("id","currenty"); 
    $("#contenty #tab1").fadeIn(); 

    $('#tabs a').click(function(e) {
        e.preventDefault();
        if ($(this).closest("li").attr("id") == "currenty"){ 
         return;       
        }
        else{             
          $("#contenty").find("[id^='tab']").hide(); 
          $("#tabs li").attr("id",""); //Reset id's
          $(this).parent().attr("id","current"); 
          $('#' + $(this).attr('name')).fadeIn(); 
        }
    });
});
</script>

and i use jquery 1.9.1 我使用jQuery 1.9.1

$(document).ready(function () {
   $("#tabs ul  li a").click(function () {
     $('#tabs ul  li a.active').removeClass('active').addClass('unactive');
     $(this).addClass('active');
   });
});

Try wrapping the code in ready event handler instead of load. 尝试将代码包装在ready事件处理程序中,而不是加载。

Try, 尝试,

$("#tabs ul  li a").click(function () {
    $("#tabs ul li a").removeClass("active").addClass('unactive')
    $(this).toggleClass("active unactive");
});

DEMO 演示

The most minimal approach would be, 最小的方法是

$("#tabs ul  li a").click(function () {
    var active_link = $("#tabs ul li a").filter('.active').removeClass('active'); // filter elements only with class active and remove it.
    $(this).toggleClass("active"); // toggle active class of this element
});

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM