jquery addclass 样式不起作用,选择器错误?
这是我想要设置样式的列表项:
$("#leistungen a").addClass("brown");
这对任何一个都不起作用:
$("#leistungen").addClass("brown");
我的CSS代码只是
.brown {
color:brown;
}
我不知道出了什么问题,希望你能帮助我: )
多谢!
<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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Saying
适用于 #leistungen 内的所有文本,但不适用于锚标记。锚点具有必须覆盖的默认样式。你的写法是正确的
做一个测试来确保你的Javascript正在执行,方法是将其更改为
如果它消失,那么你就知道你的Javascript正在正确的元素上工作。接下来,尝试使您的选择器更加具体(也许其他东西会覆盖您的更改)。
如果这不起作用,请确保 javascript 已执行,然后检查该元素。有新的班级吗?如果是这样,那么问题出在 CSS 而不是 Javascript。检查“.brown”样式周围的 CSS 文件以查找语法错误。以前的样式中缺少 } 就可以做到这一点。
尝试使用“#4C3F12”而不是“棕色”一词作为颜色。
让我知道这些是否有帮助!
Saying
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
Do a test to make sure your Javascript is executing by changing it to
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).
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.
Let me know if any of these help!
区分大小写——你用大写的 L 命名你的锚点。
在第一种情况下,你还需要使用类选择器,而不是 id 选择器
注意——我会避免匹配的类和 id (区分大小写或不区分大小写)。
HTML ID 属性 文档(强调我的):
Case-sensitivity -- you named your anchor with an upper-case L.
In the first case, also, you'd need to use a class selector, not an id selector
Note -- I'd avoid classes and ids that match (with or without case sensitivity).
HTML ID Attribute docs (emphasis mine):
你安装了萤火虫吗?在 Firebug 中,您需要检查是否添加了该类。如果已添加,则刷新浏览器缓存,否则检查您的 JavaScript。
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.
您可能对 CSS 特异性< /a> 这里。如果“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:
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:
可能是你的某个地方有冲突的 css 定义。
如果您确定 javascript 正在执行,请检查以下内容:
尝试将声明更改为:
或者
.brown a {color: Brown;}
取决于您是否使用
$("#leistungen a").addClass("brown");
或$("#leistungen").addClass (“棕色”);
It might be that you have a conflicting css definition somewhere.
If you are certain that the javascript is being executed, then check this:
Try changing the declaration to:
Or
.brown a {color: brown;}
Depending on if you use
$("#leistungen a").addClass("brown");
or$("#leistungen").addClass("brown");