css样式表比jquery addClass强吗?

发布于 2024-08-05 23:50:27 字数 399 浏览 4 评论 0原文

嘿伙计们,这件事让我发疯。我在样式表中定义:

ul#menuCnt li a {
   color:"red";
}

如果我想制作悬停效果,使用 jquery 这不会改变任何颜色。

$("ul#menuCnt li a").hover(function() {
        $(this).addClass("brown");
    }, function() {
        $(this).removeClass("brown");
    });

我真的很困惑。如果我没有在 css 样式表中定义颜色,则悬停效果将起作用。

希望你能帮助我。你们学习 jquery 和 css 对我帮助很大:)

谢谢!

Hey guys this thing drives me crazy. I defined in my stylesheet:

ul#menuCnt li a {
   color:"red";
}

If I want to make a hover effect, using jquery this won't change any color.

$("ul#menuCnt li a").hover(function() {
        $(this).addClass("brown");
    }, function() {
        $(this).removeClass("brown");
    });

I am really confused. If I don't define a color in my css stylesheet the hovereffect works.

Hope you can help me. You guys helped me so much by learning jquery and css :)

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

[浮城] 2024-08-12 23:50:27

根据CSS优先级,如果你的类.brown在你的CSS文件中定义如下:

.brown{}

里面的规则不会覆盖你的#选择器中的相同规则。

您可以使用 !important 关键字使其覆盖它们。

.brown {
    color: brown !important;
}

尽管这不是最好的解决方案,但它会起作用...您使用的 !important 越少,您的代码就越容易阅读。

请参阅http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html

According to css priorities, if your class .brown is defined like this in your css file :

.brown{}

The rules inside will not override same rules in your # selector.

You can make it override them using the !important keyword.

.brown {
    color: brown !important;
}

Altough this is not the best solution here, it will work... The less you use !important, the more your code will be easy to read.

See http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html.

猫九 2024-08-12 23:50:27

你的 CSS 中有 .brown 选择器吗?此外,a 标签可能是一个糟糕的选择,因为它内置了一个非常好的 :hover 选择器。

Do you have a .brown selector in your CSS? Also an a tag might be a bad choice because it has a very good :hover selector build in.

我的奇迹 2024-08-12 23:50:27

我没有看到你提到使用 css 来执行此操作的问题,所以为什么不尝试:

ul#menuCnt li a:hover {
   color:"brown";
}

编辑:它实际上应该在 ie6 中工作,因为它是一个锚:) 它比使用 javascript 执行此操作要快得多。

I don't see you mention a issue with using css to do this, so why not try:

ul#menuCnt li a:hover {
   color:"brown";
}

Edit: It should actually work in ie6, seeing as it is an anchor :) It would be much faster than using javascript to do it.

纸短情长 2024-08-12 23:50:27

这里有一个非常具体的元素说明符。这被认为比简单的 .brown 类选择器更重要,因此它没有太大作用。

例如,请参阅网站更多信息

You have a very specific element specifier here. That's considered more important than a simple .brown class selector, and thus it doesn't have much effect.

See for example this site for more info

初见终念 2024-08-12 23:50:27

为什么不使用 CSS :hover 伪类?例如:

#menuCnt li a {
    color: "red";
}

#menuCnt li a:hover {
    color: "brown";
}

此外,在选择器中使用“UL”不是必要的。页面上不可能存在 ID 为“menuCnt”的其他元素,因为 ID 属性在 DOM 中必须是唯一的。

Why don't you use the CSS :hover pseudoclass? For example:

#menuCnt li a {
    color: "red";
}

#menuCnt li a:hover {
    color: "brown";
}

Also, the usage of "UL" in the selector shouldn't be necessary. There's no way that there are other elements on the page with the ID "menuCnt" as the ID attribute must be unique in the DOM.

泛滥成性 2024-08-12 23:50:27

我用 IE/FF 做了一些测试,发现在样式表 (ul#menuCnt) 中包含元素的 ID 可以防止 JQuery 覆盖该样式 - 至少使用我使用的方法(toggleClass、addClass、removeClass 等)。

如果将类分配给标签中的元素(而不是在样式表中指定 ID),则可以使用 JQuery 覆盖它。

如果从样式中删除 ID,则可以使用 JQuery 覆盖它(不是很有用,但对于演示行为很有用)。

事实上,如果您将 ID 保留在样式表中并尝试在标记内分配不同的类,您仍然会在样式表中看到与 ID 关联的类。

笔记:
文森特的帖子说明了我们为什么会看到这种行为。

我也同意 exhuma 在 CSS 中使用 :hover 的建议。

I did some testing with IE/FF and discovered that including the element's ID in your stylesheet (ul#menuCnt) prevents JQuery from overriding that style - at least with the methods I employed (toggleClass, addClass, removeClass, etc.).

If you assign the class to the element in the tag (instead of specifying the ID in the stylesheet) you can override it with JQuery.

If you remove the ID from the style you can override it with JQuery (not really useful but good for demonstrating behavior).

In fact, if you leave the ID in your stylesheet and attempt to assign a different class within the tag, you still see the class associated with the ID in the stylesheet.

NOTE:
Vincent's post suggests why we see this behavior.

I also agree with exhuma's suggestion to use :hover in your CSS.

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