从 JavaScript 设置 CSS 伪类规则
我正在寻找一种方法来更改 JavaScript 中伪类选择器(例如:link、:hover 等)的 CSS 规则。
JS 中的 CSS 代码类似:a:hover { color: red }
。
我在其他地方找不到答案; 如果有人知道这是浏览器不支持的东西,那也将是一个有用的结果。
I'm looking for a way to change the CSS rules for pseudo-class selectors (such as :link, :hover, etc.) from JavaScript.
So an analogue of the CSS code: a:hover { color: red }
in JS.
I couldn't find the answer anywhere else; if anyone knows that this is something browsers do not support, that would be a helpful result as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您不能单独在特定元素上设置伪类的样式,就像在内联 style="..." 属性中不能有伪类一样(因为没有选择器)。
您可以通过更改样式表来实现此目的,例如通过添加规则:
假设您想要影响的每个元素都有一个唯一的 ID 以允许选择它。
理论上,您想要的文档是 http://www.w3。 org/TR/DOM-Level-2-Style/Overview.html 这意味着您可以(给定预先存在的嵌入或链接样式表)使用如下语法:
IE,当然,需要它自己的语法:
Older and次要浏览器可能不支持这两种语法。 动态样式表的修改很少被采用,因为它很难正确执行,很少需要,而且历史上很麻烦。
You can't style a pseudo-class on a particular element alone, in the same way that you can't have a pseudo-class in an inline style="..." attribute (as there is no selector).
You can do it by altering the stylesheet, for example by adding the rule:
assuming each element you want to affect has a unique ID to allow it to be selected.
In theory the document you want is http://www.w3.org/TR/DOM-Level-2-Style/Overview.html which means you can (given a pre-existing embedded or linked stylesheet) using syntax like:
IE, of course, requires its own syntax:
Older and minor browsers are likely not to support either syntax. Dynamic stylesheet-fiddling is rarely done because it's quite annoying to get right, rarely needed, and historically troublesome.
我为此构建了一个小型库,因为我确实认为在 JS 中操作样式表有有效的用例。 原因是:
I threw together a small library for this since I do think there are valid use cases for manipulating stylesheets in JS. Reasons being:
只需将 css 放入模板字符串中即可。
然后创建一个样式元素并将字符串放置在样式标签中并将其附加到文档中。
特异性会解决剩下的问题。 然后您可以动态删除和添加样式标签。 这是库和 DOM 中样式表数组的一个简单替代方案。 快乐编码!
Just place the css in a template string.
Then create a style element and place the string in the style tag and attach it to the document.
Specificity will take care of the rest. Then you can remove and add style tags dynamically. This is a simple alternative to libraries and messing with the stylesheet array in the DOM. Happy Coding!
处理跨浏览器问题的函数:
A function to cope with the cross-browser stuff:
我的技巧是使用属性选择器。 通过 javascript 设置属性更容易。
css
javascript
html
My trick is using an attribute selector. Attributes are easier to set up by javascript.
css
javascript
html
您可以考虑的一种选择是使用 CSS 变量。 这个想法是将要更改的属性设置为 CSS 变量。 然后,在 JS 中更改该变量的值。
请参阅下面的示例
One option you could consider is using CSS variables. The idea is that you set the property you want to change to a CSS variable. Then, within your JS, change that variable's value.
See example below
还有另一种选择。 不要直接操作伪类,而是创建模拟相同事物的真实类,例如“悬停”类或“访问”类。 使用通常的“.”来设计类的样式。 语法,然后当适当的事件触发时,您可以使用 JavaScript 在元素中添加或删除类。
There is another alternative. Instead of manipulating the pseudo-classes directly, create real classes that model the same things, like a "hover" class or a "visited" class. Style the classes with the usual "." syntax and then you can use JavaScript to add or remove classes from an element when the appropriate event fires.
您可以在不同的 CSS 文件中设置不同的规则,然后使用 Javascript 关闭一个样式表并打开另一个样式表,而不是直接使用 javascript 设置伪类规则。 A List Apart 中描述了一种方法(更多详细信息请参见 qv.)。
将 CSS 文件设置为,
然后使用 javascript 在它们之间切换:
Instead of directly setting pseudo-class rules with javascript, you can set the rules differently in different CSS files, and then use Javascript to switch one stylesheet off and to switch another on. A method is described at A List Apart (qv. for more detail).
Set up the CSS files as,
And then switch between them using javascript:
正如已经指出的,这不是浏览器支持的东西。
如果您没有动态地提出样式(即从数据库或其他东西中提取它们),您应该能够通过向页面主体添加一个类来解决这个问题。
css 看起来像:
改变它的 javascript 看起来像:
As already stated this is not something that browsers support.
If you aren't coming up with the styles dynamically (i.e. pulling them out of a database or something) you should be able to work around this by adding a class to the body of the page.
The css would look something like:
And the javascript to change this would be something like:
在jquery中你可以轻松设置hover伪类。
In jquery you can easily set hover pseudo classes.
这是一个包含两个函数的解决方案: addCSSclass 向文档添加一个新的 css 类,toggleClass 将其打开
该示例显示向 div 添加自定义滚动条
here is a solution including two functions: addCSSclass adds a new css class to the document, and toggleClass turns it on
The example shows adding a custom scrollbar to a div
如果你使用 REACT ,有一个叫做 radium 的东西。 它在这里非常有用:
If you use REACT , There is something called radium. It is so useful here :