javascript - Set class to active and remove it when another menu item is selected bug - Stack Overflow

I tested out the script below in jsfiddle and it works fine, can someone guide me how to fix it? This i

I tested out the script below in jsfiddle and it works fine, can someone guide me how to fix it? This is the url that I need it working in, the wizard style menu at the top right should should have each item set to active when clicked and then removed when another menu item is clicked: .html

Here is the code I am using for this:

    <script type="text/javascript">
      $('.wizard-steps div').click(function(e) {
            e.preventDefault();
            $('a').removeClass('active');
            $(this).addClass('active');
        });
    </script>

I tested out the script below in jsfiddle and it works fine, can someone guide me how to fix it? This is the url that I need it working in, the wizard style menu at the top right should should have each item set to active when clicked and then removed when another menu item is clicked: http://morxmedia./clients/temp/45p/index_vertical.html

Here is the code I am using for this:

    <script type="text/javascript">
      $('.wizard-steps div').click(function(e) {
            e.preventDefault();
            $('a').removeClass('active');
            $(this).addClass('active');
        });
    </script>
Share Improve this question asked Jun 27, 2012 at 8:39 StephenStephen 7173 gold badges14 silver badges33 bronze badges 1
  • given link does not add active class to any div – xkeshav Commented Jun 27, 2012 at 8:45
Add a ment  | 

5 Answers 5

Reset to default 3

You are binding the click event to div elements when you should bind them to a elements like so

$(document).ready(function(){
    $('.wizard-steps > div > a').click(function(e) {
        e.preventDefault();
        $('a').removeClass('active');
        $(this).addClass('active');
    });
});

Try this.

$(function() {
    $('.wizard-steps div').click(function(e) {
       e.preventDefault();
       $(this).addClass('active').siblings().removeClass('active');
    });
});

better to include that on a ready

$(document).ready(function() {
  $('.wizard-steps div').click(function(e) {
        e.preventDefault();
        $('a').removeClass('active');
        $(this).addClass('active');
    });
});

As far as I can see (in your CSS). The class active should go on the div under wizard-steps and the parent of the a-tag.

Try this:

<script type="text/javascript">
$('.wizard-steps div a').click(function(e) {
    if (e.preventDefault)
        e.preventDefault();
    else
        e.stop();

    $('.wizard-steps div').removeClass('active');
    $(this).parent().addClass('active');
});
</script>

It can be done in another way using jquery using .not()

Jquery Code:

$('.wizard-steps div').click(function() {
          $('.wizard-steps div').not(this).removeClass('active');
          $(this).addClass('active');
});

Demo: http://jsfiddle/surendraVsingh/PLbbr/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745443735a4627948.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信