使用 jquery 更改文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2941679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:35:26  来源:igfitidea点击:

Change text color using jquery

jquery

提问by Hacker

I have a radio button like this

我有一个这样的单选按钮

<input type='radio' name='specific_consultant' id='specific_consultant_no' 
                                                                      value='no'>No</input>

on .on clickof this radio button , I need to change text color like No .. how do i do it..

.on click此单选按钮上,我需要更改文本颜色,例如 No .. 我该怎么做..

回答by Darin Dimitrov

You could use the cssfunction:

您可以使用css函数:

$(function() {
    $('#specific_consultant_no').click(function() {
        $(this).css('color', 'red');
    });
);

回答by Andreas Niedermair

$(document).ready(function() {
    var button = $('#specific_consultant_no');
    button.click(function() {
        button.css('color', 'FOO');
    });
});

if you want to reuse the method, you can do eg.

如果你想重用这个方法,你可以做例如。

var colorMethod = function() {
    $(this).css('color', 'FOO');
};
$(document).ready(function() {
    var button = $('#specific_consultant_no');
    button.click(colorMethod);
});

you can also use the addClassand removeClassmethods, to be more flexible!

您还可以使用addClassremoveClass方法,更加灵活!

回答by Andreas Niedermair

$("#myButton").on('click', function () {
        $(this).toggleClass('green');
        $("#result").toggle();
});