简体   繁体   English

仅在使用jQuery的单击持续时间内将活动类添加到元素onclick

[英]Add active class to an element onclick just for the duration of click using jquery

I'm trying to add active class to an element when someone clicks on it and then auto remove it immediately. 我试图在有人单击某个元素时将活动类添加到元素,然后立即将其自动删除。

I've already tried with below methods but no luck yet - 我已经尝试过以下方法,但是还没有运气-

Method - 1 方法-1

setTimeout(function() {
    $('.classname').on('click', function() {
        $('.classname').addClass('active');
    }, 1000);
});

Method - 2 方法-2

$('.classname').on('click', function() {
    $( ".classname" ).switchClass( "active", "no-active", 1000 );
});

You're correct in that you need to use setTimeout() , however the logic is a little off. 正确的是,您需要使用setTimeout() ,但是逻辑有些偏离。

Firstly you need to use setTimeout() within the click handler. 首先,您需要点击处理程序中使用setTimeout() You will then need to call removeClass() on a reference to the clicked element when the timer completes. 然后,当计时器完成时,您将需要在对clicked元素的引用上调用removeClass() Try this: 尝试这个:

 $('.classname').on('click', function() { var $el = $(this).addClass('active'); setTimeout(function() { $el.removeClass('active'); }, 1000); }); 
 div { transition: color 0.3s, background-color 0.5s; } .active { color: #FFF; background-color: #C00; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="classname">foo</div> <div class="classname">foo</div> <div class="classname">foo</div> <div class="classname">foo</div> <div class="classname">foo</div> 

However, from the image you posted it looks like all you may need is to add an :active state. 但是,从您发布的图像看来,您可能需要添加:active状态。 This is applied to the element while the mouse button is held down on it: 在按住鼠标按钮的同时将其应用于元素:

 div:active { background-color: #C00; color: #FFF; } 
 <div>Click me</div> 

I think that is is better to be done with just CSS: using :active so it will be something like this: 我认为最好仅使用CSS来完成:使用:active这样的话:

 button { background-color:red; } button:active { background-color:green; } 
 <button type="button"> Click Here </button> 

or with a little delay: 或稍有延迟:

 button { border: 1px solid #bada55; background-color:red; transition: .50s all; transition-delay: 1s; } button:active { background-color:green; transition-delay: 0s; } 
 <button type="button"> Click Here </button> 

try this one 试试这个

before setTimeout function call you add the class and after some seconds it will remove using settimeout setTimeout函数调用之前,您添加了class ,几秒钟后它将使用settimeout删除

 $('.classname').click(function() { $('.classname').addClass('active'); setTimeout(function() { $('.classname').removeClass('active'); }, 500); }); 
 .active { width: 30px; height: 20px; background: blue; color: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="classname"> test </div> 

and for switchclass 对于switchclass

You are missing jQuery UI most likely, as switchClass is part of jQuery UI, and not the jQuery library. 您很可能会缺少jQuery UI,因为switchClass是jQuery UI的一部分,而不是jQuery库的一部分。

 $(function () { setInterval(function () { $('.classname').addClass('active'); }, 1000); setInterval(function () { $('.classname').removeClass('active'); }, 2000); }); 
 .active{ color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script> <div class="classname">Hello</div> 

try this one 试试这个

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

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