开发者

Setting a class to a label

Trying to set this label to red

I've got the following class:

.redErrorText
{
    color: #b11010; 
}

The following label:

<label for="txtNumber" id="lblBlue">Number 1</label>

And then this code runs to try and set the label to red:

var blueLabel = "#lblBlue";
$(blueLabel).addClass("redErrorText");

I can't figure out what I could be doing wrong here, looks fine to m开发者_如何学Goe.


Are you using webforms? because if you are then the ID you're expecting may not be the one you have. it may have some dopey stuff after it.

try looking at it in source view from the browser and check the it.

also try something like this if you are using webforms;

<label class='MyRedClass' for="txtNumber" id="lblBlue">Number 1</label>
$('.MyRedClass').addClass("redErrorText");

if it's ok then are you loading jQuery?


With the code you've provided, there could be a number of problems.

  • Is the ID you're refering to unique? jQuery will have issues with the same id defined multiple times.
  • Is the javascript being called before the #lblBlue element is ready?
  • Does $(blueLabel) return a valid reference? try:

    console.log($(blueLabel))

Make sure jquery is finding the element correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜