JQUERY - 在点击后向BUTTON元素添加CSS类 [英] JQUERY - add CSS class to BUTTON element after click

查看:113
本文介绍了JQUERY - 在点击后向BUTTON元素添加CSS类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按钮后永久更改按钮的背景颜色。使用以下代码,颜色只有在按钮被激活/被点击时才会改变。一旦鼠标按钮被释放,添加的clicked类不再应用到元素。

I want to change the background color of a button permanently after it is clicked. Using the following code the color only changes while the button is active/being clicked. Once the mouse button is released the added "clicked" class is no longer applied to the element.

将以下代码应用于INPUT标记而不是BUTTON标签方式我需要它。但是,我使用BUTTON标签,因为我想要的值不同于按钮内的文本。

Applying the following code to an INPUT tag instead of a BUTTON tag works the way I need it to. However, I'm using a BUTTON tag since I want the value to be different from the text inside the button.

我需要改变什么?

- 谢谢。

 HTML
 <button class="inactive" value="test">Button</button>

 CSS
 .inactive {background-color:gray;}
 .clicked {background-color:orange;}

 JS
 $(document).ready(function(){
      $('button').click(function(){
           $(this).addClass('clicked');
      });
 );


推荐答案

唯一的问题是你缺少花括号: http://jsfiddle.net/AaKuf/

The only problem is that you are missing a curly brace: http://jsfiddle.net/AaKuf/

 $(document).ready(function(){
      $('button').click(function(){
           $(this).addClass('clicked');
      });
 }); //<==here

这篇关于JQUERY - 在点击后向BUTTON元素添加CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆