最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I remove toggleClass on click and when any other element and white space is clicked? - Stack Overflow

programmeradmin4浏览0评论

I am using JQuery toggleClass to change the color of a button when it's clicked. When it's clicked again it should revert to the original color AND when the mouse is clicked anywhere else including white space, it should revert to the original color.

I couldn't figure out how to do it but below are two snippets that I managed to find and plug stuff in.

The following snippet will change the color when button is clicked but when clicked again it doesn't revert to original color but when any other element or a white space is clicked, the button color reverts to the original color:

  (function () {
$('.button_is').click(function(evt) {
    evt.stopPropagation(); //stops the document click action
    $(this).siblings().removeClass('button_addition');
    $(this).toggleClass('button_addition');
});


$(document).click(function() {
    $('.button_is').removeClass('button_addition'); //make all inactive
});
});

The following snippet will toggle class when the element is clicked and when it's clicked again it reverts to it's original state but wont' revert to original color if clicked elsewhere:

 $(document).ready(function () {

            $(".button_is").click(function () {
                $(this).toggleClass("button_addition");
            });
        });

Which of these two snippets can I utilize to make sure that when the button is clicked the color changes and when it's clicked again it reverts to the original color AND when other elements or white space is clicked it reverts to the original color?

I am using JQuery toggleClass to change the color of a button when it's clicked. When it's clicked again it should revert to the original color AND when the mouse is clicked anywhere else including white space, it should revert to the original color.

I couldn't figure out how to do it but below are two snippets that I managed to find and plug stuff in.

The following snippet will change the color when button is clicked but when clicked again it doesn't revert to original color but when any other element or a white space is clicked, the button color reverts to the original color:

  (function () {
$('.button_is').click(function(evt) {
    evt.stopPropagation(); //stops the document click action
    $(this).siblings().removeClass('button_addition');
    $(this).toggleClass('button_addition');
});


$(document).click(function() {
    $('.button_is').removeClass('button_addition'); //make all inactive
});
});

The following snippet will toggle class when the element is clicked and when it's clicked again it reverts to it's original state but wont' revert to original color if clicked elsewhere:

 $(document).ready(function () {

            $(".button_is").click(function () {
                $(this).toggleClass("button_addition");
            });
        });

Which of these two snippets can I utilize to make sure that when the button is clicked the color changes and when it's clicked again it reverts to the original color AND when other elements or white space is clicked it reverts to the original color?

Share Improve this question asked Apr 16, 2013 at 18:49 AAAAAA 3,16811 gold badges55 silver badges73 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

You may try this

$(document).ready(function () {

    $(".button_is").click(function (e) {
        e.stopPropagation();
        $(this).toggleClass("button_addition");
    });

    $(document).click(function() {
        $(".button_is.button_addition").trigger("click");
    });    

});

DEMO.

Try this

 $(function() {
        $('.button_is').on('click',
        function(event) {
            event.stopPropagation();
            $('.button_is').toggleClass('button_addition');
        });
        $('html').click(function() {
            $('.button_is').removeClass('button_addition');
        });
    });

Fiddle: http://jsfiddle/9ZKfq/

$(document).ready(function() {

    $(document).click(function(jEvent) {    

        if (!$(jEvent.target).hasClass('button_is')) 
        {
           $('.button_is').removeClass('button_addition');
        }
        else
        {
            $(jEvent.target).toggleClass('button_addition');
        }

    });

});

Try this -

    $(document).ready(function () {
        $(".button_is").click(function () {
            $(this).toggleClass("button_addition");
        });

        $(document).click(function(event) {
          if(!$(event.target).is('.button_is')){
            $('.button_is').removeClass('button_addition'); //make all inactive
          }
        });
  });

Your first snip works for me.

Maybe your HTML is the problem?

It's worth noting that there is a typo in your code (you're missing the $ on the ready handler).

Look at this: http://jsfiddle/URrVf/ It works on Chrome, Firefox and IE9.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论