views:

2301

answers:

5

<li id="leistungen"><a href="#Leistungen" onclick="sitemapChangeSubMenu('leistungen','leistungencontent','leistungen'); return false;">LEISTUNGEN</a> </li>

This is the list-item which I want to style with:

$("#leistungen a").addClass("brown");

This doesn't work for either:

$("#leistungen").addClass("brown");

my css code is just

.brown {
   color:brown;
}

I don't know what is wrong, hopefully you can help me :)

thanks a lot!

+1  A: 

Case-sensitivity -- you named your anchor with an upper-case L.

$("#Leistungen").addClass("brown");

In the first case, also, you'd need to use a class selector, not an id selector

$(".leistungen a").addClass("brown");

Note -- I'd avoid classes and ids that match (with or without case sensitivity).

HTML ID Attribute docs (emphasis mine):

The ID attribute uniquely identifies an element within a document. No two elements can have the same ID value in a single document. The attribute's value must begin with a letter in the range A-Z or a-z and may be followed by letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). The value is case-sensitive.

tvanfosson
thanks for your fast replys :)unfurtunately the id and the selector are the same(both smallcaps). :(
why do i need a class selector my? i have an id to style.
In the first form you are selecting an anchor that is a descendent of an element with id "leistungen" -- you don't have any such anchor. You do have an anchor that's a descendent of a li with **class** "leistungen" and would need a class selector if using the descendent form. In the second case, your code shows that the id of the anchor starts with an Uppercase letter, not lowercase. Since ids are case-sensitive, you need to use the same exact casing in your id selector to chose that particular element. Have you tried either of my suggestions?
tvanfosson
A: 

You have firebug installed? In firebug you'll need to check whether the class is being added or not. If its being added, then refresh your browser cache, otherwise check your javascript.

Arpit Tambi
i followed your hint. the class is added but there's another thing the "a" tag does get -> style="color: rgb(76, 63, 18);but why?
style="color: rgb(76, 63, 18); sets the background color to brown(only for test purposes, but the text remains blue(as before)
+1  A: 

Saying

$("#leistungen").addClass("brown");

will work on all text inside #leistungen, but not on anchor tags. Anchors have a default style that must be overridden. You are correct in writing

$("#leistungen a").addClass("brown");

Do a test to make sure your Javascript is executing by changing it to

$("#leistungen a").hide();

If it disappears, then you know that your Javascript is working on the correct element. Next, try being more specific with your selector (maybe something else is overriding your change).

$("li#leistungen a").addClass("brown");

If that doesn't work, make sure the javascript has executed and then inspect the element. Does it have the new class? If so, then the problem is with the CSS rather than the Javascript. Check your CSS file surrounding the ".brown" style to look for syntax errors. A missing } from the previous style would do it.

Try using "#4C3F12" instead of the word "brown" as a colour.

.brown {
  color: #4C3F12;
}

Let me know if any of these help!

gavinblair
okay, that helped thanks :) $("#leistungen a").addClass("brown");but only if i call click a button within my <ul>if i click a button in another div the addclass won't work.
everything works now :)another script influenced the css. :(Thanks for your great help!
A: 

You might have a problem with CSS specificity here. If the original color of your 'a' tag is specified very specific, the new definition might not work. For example:

body #leistungen a {
   color: red;
}

.brown {
   color: brown;
}

In this case the red color has precedence over the brown color, because the first definition is more specific than the last one. Try defining the brown class more specifically:

body #leistungen a.brown {
   color: brown;
}
Edwin V.
A: 

It might be that you have a conflicting css definition somewhere. If you are certain that the javascript is being executed, then check this:

  1. Since it is an ID, make sure that that ID is not assigned to another tag as well.
  2. Do you have any other css declarations that applies on a tags?

Try changing the declaration to:

a.brown{ color: brown;}

Or .brown a {color: brown;}

Depending on if you use $("#leistungen a").addClass("brown"); or $("#leistungen").addClass("brown");

Jimmy Stenke