jQuery 使用jquery更改标签颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11809378/
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 10:58:21  来源:igfitidea点击:

Change color of label using jquery?

jqueryhtmlsubmitlabel

提问by vini

I wanted to change the color of the label to Red on button click

我想在单击按钮时将标签的颜色更改为红色

However the code isn't working everything seems to be right

但是代码不起作用一切似乎都是正确的

    <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>
    </title>
    <script type="text/javascript">
    function changeColor(id, newColor) {

var labelObject = document.getElementById(id);

$("#" + id).css("color", newColor);

}
    </script>
</head><body>
<form id="frm2">

<label for="model">Male</label>

<input type="text" name="cars" id="model" />

<br />

<label for="female">Female</label>

<input type="text" name="cars" id="color" />

</form>

<input type="button" value="Change Label Color" onclick="return changeColor('label', 'red')" />

    </body>
</html>

Please help

请帮忙

回答by rexmac

You're passing 'label' as the id parameter of your changeColorhandler, but there is no element with that ID in the HTML you provided. You'll need to add some IDs to your labels and pass those in the onclick handler. For example:

您将“标签”作为changeColor处理程序的 id 参数传递,但您提供的 HTML 中没有具有该 ID 的元素。您需要向标签添加一些 ID,并将它们传递到 onclick 处理程序中。例如:

<label for="model" id="label1">Male</label>
<input type="text" name="cars" id="model" />

<input type="button" value="Change Label Color" onclick="return changeColor('label1', 'red')" />

An alternative would be to pass the ID of the input element instead as they already have IDs assigned to them. You would then need to modify your changeColorhandler as follows:

另一种方法是传递输入元素的 ID,因为它们已经分配了 ID。然后,您需要changeColor按如下方式修改您的处理程序:

function changeColor(inputId, newColor) {
  $("#" + inputId).prev().css("color", newColor);
}

Edit:Here is a jsFiddledemonstrating my second example.

编辑:这是一个jsFiddle演示我的第二个例子。

回答by Robin Maben

$('input[type="button"]').click(function(){
     changeColor('labelCity' , 'red');
}); 

function changeColor(id, newColor) {

   $("#" + id).css("color", newColor);

}  

回答by Manmohan

Here is a simple example to change the color of a label text to red using jQuery.

这是一个使用 jQuery 将标签文本的颜色更改为红色的简单示例。

    <script src="Scripts/jquery-1.9.0.min.js"></script>
    <script>

        $(document).ready(function () {
            $('#btnChangeColor').click(function () {
                $('#lbl1').css("color", "red");
            })
        })

        
    </script>
<body>
    <form id="form1" runat="server">

        <label id="lbl1" >Hello friends save girl child</label>
        <br /><br /><br />

        <input type="button" id="btnChangeColor" value="Change Color" />

    </form>
</body>