在 JQuery 中访问伪类的 CSS 样式?

发布于 2024-08-30 18:56:01 字数 254 浏览 9 评论 0原文

如果我像这样定义按钮的向上和上方状态:

.button {color:red;}
.button:hover {color:blue;}

How can I get all the hover state styles for an element using JQuery?

类似于 $(".button:hover").css('color');...

If I define the up and over states of a button like this:

.button {color:red;}
.button:hover {color:blue;}

How can I get all the hover state styles for an element using JQuery?

Something like $(".button:hover").css('color');...

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

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

发布评论

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

评论(1

审判长 2024-09-06 18:56:01

这是不可能直接实现的,至少在没有悬停的情况下是不可能的。您可以获得的最接近的是:

$('.button').mouseenter(function() {
  alert($(this).css('color'));
});

但是...这需要将鼠标悬停在元素上,根据您的问题,这听起来不像目标。由于悬停的完成方式,从来没有任何理由在 javascript 中公开此信息,浏览器 css/渲染引擎会处理所有这些信息...因为这可能是我多年来第二次看到这个问题,所以只是那些很少使用、从未添加的东西之一,就像其他所有语言/平台都有上千个例子一样。

抱歉,答案很糟糕,但是“它不会那样做”在某些时候是所有事情的答案,但在这种情况下可能会保持这种状态。

This isn't possible directly, at least not without hovering. The closest you can get is:

$('.button').mouseenter(function() {
  alert($(this).css('color'));
});

But...this requires hovering over the element, which by your question doesn't sound like the goal. Because of the way hovers are done, there was never any reason to expose this information in javascript, the browser css/rendering engine handles all of it...since this is maybe the second time I've seen this asked in years, it's just one of those rarely-used so never-added things, like every other language/platform has a thousand examples of.

Sorry the answer sucks, but "it doesn't do that" is the answer for everything at some point, but will probably remain that way in this case.

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