简体   繁体   English

使用jquery添加css字体颜色

[英]Add css font color with jquery

Sure it's a simple question, but I can't fix it, can somebody help me? 当然这是一个简单的问题,但我无法解决它,有人可以帮助我吗?

This is the original line 这是原始的一行

$('.winning-col', this).text($('td.win', this).length);

and this is where I came up with, surely not correct. 这就是我想出来的地方,当然不正确。

$('.winning-col', this).text.css('color', 'pink'($('td.win', this).length));

You could do it the quick-way: 你可以快速做到这一点:

$(".winning-col", this)
  .text($("td.win", this).length)
  .css("color", "pink");

But ideally, you would use .addClass instead: 但理想情况下,您可以使用.addClass:

$(".winning-col", this)
  .text($("td.win", this).length)
  .addClass("hilighted");

Where 哪里

.hilighted { color: pink; }

Thought I'd add a little extra information here, just in case you're not aware of it. 以为我会在这里添加一些额外的信息,以防万一你不知道。 When you use the .css() function, you can also specify the arguments as what's called an object literal, which basically means something in this format: 当你使用.css()函数时,你也可以将参数指定为所谓的对象文字,这基本上就是这种格式的东西:

{objectVarName1: objectVarValue1, objectVarName2: objectVarValue2}

You can also do it like this: 你也可以这样做:

{"objectVarName1": objectVarValue1, "objectVarName2": objectVarValue1}

With the .css() function, you can do this: 使用.css()函数,您可以执行以下操作:

$("#the_item_id").css({backgroundColor: "#333", color: "#FFF"});

If the variable names you're passing aren't in quotes, you have to do it in camel-case like I did above, which means that the first word of the name of the CSS property is lowercase, but every word after that is in caps (so the CSS property background-color becomes backgroundColor ). 如果您传递的变量名不在引号中,则必须像我上面所做的那样以驼峰形式执行,这意味着CSS属性名称的第一个单词是小写,但之后的每个单词都是在大写(因此CSS属性background-color变为backgroundColor )。 To do the equivalent of the above in the form where you put the name of the variable in the object in quotes, you would just do this: 要在以引号将对象的名称放在对象中的形式中执行上述操作,您只需执行以下操作:

$("#the_item_id").css({"background-color": "#333", "color": "#FFF"});

Just wanted to point out that you don't have to chain multiple calls to .css() together, that you can do all of your CSS changes at once. 只是想指出你不必将多个调用链接到.css(),你可以一次完成所有的CSS更改。 ;) ;)

Try this: 试试这个:

$('.winning-col', this).text($('td.win', this).length).css('color', 'pink');

Each function call, even in jQuery, is still separate. 即使在jQuery中,每个函数调用仍然是独立的。 The first call is to .text() to change the text. 第一个调用是.text()来更改文本。 The second is to .css() to change the CSS. 第二个是.css()来改变CSS。 It just so happens that in jQuery each function call of this type returns a jQuery object on which you can call more functions, thus nicely chaining them all together. 事实上,在jQuery中,每个这种类型的函数调用都会返回一个jQuery对象,您可以在其上调用更多函数,从而很好地将它们链接在一起。

$('.winning-col', this).text($('td.win', this).length).css('color', 'pink');

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

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