jquery addclass 样式不起作用,选择器错误?

发布于 2024-08-05 07:10:04 字数 460 浏览 6 评论 0原文

  • LEISTUNGEN ;
  • 这是我想要设置样式的列表项:

    $("#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 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(5

    好多鱼好多余 2024-08-12 07:10:04

    Saying

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

    适用于 #leistungen 内的所有文本,但不适用于锚标记。锚点具有必须覆盖的默认样式。你的写法是正确的

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

    做一个测试来确保你的Javascript正在执行,方法是将其更改为

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

    如果它消失,那么你就知道你的Javascript正在正确的元素上工作。接下来,尝试使您的选择器更加具体(也许其他东西会覆盖您的更改)。

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

    如果这不起作用,请确保 javascript 已执行,然后检查该元素。有新的班级吗?如果是这样,那么问题出在 CSS 而不是 Javascript。检查“.brown”样式周围的 CSS 文件以查找语法错误。以前的样式中缺少 } 就可以做到这一点。

    尝试使用“#4C3F12”而不是“棕色”一词作为颜色。

    .brown {
      color: #4C3F12;
    }
    

    让我知道这些是否有帮助!

    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!

    -柠檬树下少年和吉他 2024-08-12 07:10:04

    区分大小写——你用大写的 L 命名你的锚点。

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

    在第一种情况下,你还需要使用类选择器,而不是 id 选择器

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

    注意——我会避免匹配的类和 id (区分大小写或不区分大小写)。

    HTML ID 属性 文档(强调我的):

    ID属性唯一标识
    文档中的一个元素。没有两个
    元素可以具有相同的 ID 值
    一个文档。该属性的
    值必须以字母开头
    范围 AZ 或 az 并可遵循
    由字母 (A-Za-z)、数字 (0-9)、
    连字符(“-”)、下划线(“_”)、
    冒号(“:”)和句点(“.”)。
    值区分大小写。

    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.

    水晶透心 2024-08-12 07:10:04

    你安装了萤火虫吗?在 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.

    伴梦长久 2024-08-12 07:10:04

    您可能对 CSS 特异性< /a> 这里。如果“a”标签的原始颜色指定得非常具体,则新定义可能不起作用。例如:

    body #leistungen a {
       color: red;
    }
    
    .brown {
       color: brown;
    }
    

    在这种情况下,红色优先于棕色,因为第一个定义比最后一个定义更具体。尝试更具体地定义棕色类:

    body #leistungen a.brown {
       color: brown;
    }
    

    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;
    }
    
    九公里浅绿 2024-08-12 07:10:04

    可能是你的某个地方有冲突的 css 定义。
    如果您确定 javascript 正在执行,请检查以下内容:

    1. 由于它是一个 ID,因此请确保该 ID 没有分配给其他标签。
    2. 您还有其他适用于标签的 css 声明吗?

    尝试将声明更改为:

    a.brown{ color: brown;} 
    

    或者
    .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:

    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");

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文