简体   繁体   English

检索或更改伪元素的css规则

[英]Retrieving or changing css rules for pseudo elements

EDIT 2015-10-07 1624 CST 编辑2015-10-07 1624 CST

This question has been tagged as a possible duplicate - the reason I posted it is that none of the answers to the other question provided all the information I wanted and I want a simple and direct way to do it. 这个问题被标记为可能重复 - 我发布的原因是,其他问题的答案都没有提供我想要的所有信息,我想要一个简单而直接的方法来完成它。 I can take care of controlling the order of stylesheets and rules so that I am referencing the correct rule. 我可以控制样式表和规则的顺序,以便我引用正确的规则。 I also wanted feedback as to the feasibility of this method as regards it breaking in the future. 我还希望得到关于这种方法在将来破坏方面的可行性的反馈。

Look at the comments I posted below for additional reasons why I didn't accept the answers to the possible duplicate questions. 请查看我在下面发布的评论,原因是我没有接受可能重复问题的答案的其他原因。

** original question follows ** **原始问题如下**

I searched for a question like this and found some that address part of the question but not both retrieving and changing CSS values for pseudo elements like ::before and ::after . 我搜索了这样一个问题,发现一些问题解决了部分问题,但没有检索和更改伪元素的CSS值,例如::before::after

I have searched the web with Google before coming here and basically what I found is that there is no ideal way to do this. 在来到这里之前我已经用谷歌搜索了网页,基本上我发现的是没有理想的方法来做到这一点。

I found a way that works in FF 40, IE 9, and Chrome 45.0.2454.101 m but I'm wondering if I'm overlooking something that may cause my method to break in some instances. 我找到了一种适用于FF 40,IE 9和Chrome 45.0.2454.101 m的方法,但我想知道我是否忽视了某些可能导致我的方法在某些情况下破坏的东西。

The answers I've seen, here and other places on the web, about accessing or changing CSS values for pseudo elements, say that you can't directly access these items because they are not "part of the DOM" and "outside the DOM" 我在网上看到的关于访问或更改伪元素的CSS值的答案,说你不能直接访问这些项,因为它们不是“DOM的一部分”和“DOM之外” “

They say about the only way to change them is to create a new rule and appending it to the existing rules to override the coded value. 他们说改变它们的唯一方法是创建一个新规则并将其附加到现有规则以覆盖编码值。

Here is a snippet that demonstrates the method: 这是一个演示方法的片段:

 function changeColor () { // Flip psedo element's background color var newColor, currentColor; // Get the current color currentColor= document.styleSheets[0].cssRules[0].style.backgroundColor; // flip the color newColor = (currentColor== "red") ? "aqua" : "red"; // Change the color document.styleSheets[0].cssRules[0].style.backgroundColor = newColor; // put color in top message document.getElementById("colorIs").innerHTML = newColor; // display colors document.getElementById("displayColors").innerHTML = "Pevious color was " + currentColor + ", changed to " + newColor + "."; // Change background of button (not needed but thought I'd throw it in) document.getElementById("changeButton").style.backgroundColor = newColor; } 
 #testDiv::before { background-color: aqua; content: "psedo element "; } #changeButton { background-color: aqua; } 
 <div id="testDiv"> This divsion has an pseudo ::before element whose background color is <span id="colorIs"> aqua </span> <br> <br> Click "Display and Flip Color" to display the colors <br> and flip the color from aqua and red and vice versa. </div> <br> <form method="post"> <input id="changeButton" name="change" type="button" value="Display and Flip Color" onclick="changeColor();"> </form> <br> <div id="displayColors"> </div> 

I realize that this depends upon my knowing the order of style sheets and rules within them, but I can control that. 我意识到这取决于我知道样式表和规则的顺序,但我可以控制它。

These seems to go against the answers I've seen which say the CSS items for pseudo elements are not part of, and therefore not accessible, through the DOM. 这些似乎违背了我所看到的答案,它说伪元素的CSS项目不是DOM的一部分,因此不可访问。

Is this method possible because of browser or DOM changes that occurred after the answers I read were posted? 这个方法是否可行,因为在我阅读的答案发布后发生的浏览器或DOM更改?

What is the likelihood that this method might break in the future? 这种方法将来可能会破裂的可能性有多大?

And would those with different version of the various browsers please try the snippet and let me know if it works or not? 对于那些使用不同版本的各种浏览器的人,请尝试使用该片段并让我知道它是否有效?

Bob 短发


EDIT 2015-10-08 1352 CST 编辑2015-10-08 1352 CST

I have modified my method of accessing styles of pseudo elements to be able to directly reference the style sheet in question, regardless of the order in which it is defined. 我修改了访问伪元素样式的方法,以便能够直接引用相关样式表,而不管它的定义顺序如何。

I would change the snippet but I don't see a way to give the css "stylesheet" an id. 我会更改片段,但我没有看到一种方法给css“stylesheet”一个id。

Instead I'll tell how I would modify it. 相反,我会告诉我如何修改它。

1) Separate the CSS that defines the elements being used and put it in a separate file. 1)分离定义正在使用的元素的CSS并将其放在单独的文件中。

2) Code id= on the <link tag referencing the CSS file. 2)引用CSS文件的<link标签上的代码id =。 In this case I would use id="colorFlipFlop" 在这种情况下,我会使用id =“colorFlipFlop”

3) Change the JavaScript to reference or change a style from this: 3)将JavaScript更改为引用或更改样式:

currentColor = document.styleSheets[0].cssRules[0].style.backgroundColor;

document.styleSheets[0].cssRules[0].style.backgroundColor = newColor;

To: 至:

var beforeIndex = 0; // give a name to the index, in cssRules, of the rule we want to get and change.

var styleSheetObject = document.getElementById("colorFlipFlop"); // get a reference to the stylesheet object

currentColor = styleSheetObject.sheet.cssRules[beforeIndex].style.backgroundColor;   // get current pseudo element background color

styleSheetObject.sheet.cssRules[beforeIndex].style.backgroundColor = newColor; // set new background color

I would fully document all of this, in the CSS and Javascript, and in the HTML if I deemed it necessary, with as many comments as I thought was necessary to explain what I am doing, how I am doing it, and why I am doing it the way I am doing it - I call that the WHW of commenting. 我会完全记录所有这些,在CSS和Javascript中,如果我认为有必要,在HTML中,我认为有必要解释我在做什么,我是怎么做的,以及为什么我是按照我这样做的方式去做 - 我称之为WHW评论。

I feel this makes the functionality more manageable and more bullet proof. 我觉得这使得功能更易于管理,更具防弹性。

You no longer have to now the index of the stylesheet object, everything is neatly separated from everything else on the page, and it still provides a direct way to access and change styles of pseudo elements without creating and appending new rules. 您现在不再需要样式表对象的索引,所有内容都与页面上的其他内容完全分开,并且它仍然提供了访问和更改伪元素样式的直接方法,而无需创建和附加新规则。

Before I post this edit I will make up a file containing the CSS, JavaScript, and HTML to achieve what the snippet does to show the new method. 在发布此编辑之前,我将编写一个包含CSS,JavaScript和HTML的文件,以实现代码片段显示新方法的功能。 I will put everything in one file just to make it easier to create and FTP to the site. 我会将所有内容放在一个文件中,以便更轻松地创建和FTP到网站。 In real world code, I'd use separate CSS, JavaScript, and HTML files. 在现实世界的代码中,我使用单独的CSS,JavaScript和HTML文件。

It will be at http://thetesting.site/flipFlopColor.html 它将在http://thetesting.site/flipFlopColor.html

So - What do you think? 所以你怎么看?

Manipulating the CSSRule instead of the DOM element is an obscure but perfectly valid (and standardized ) way of changing an element's style. 操纵CSSRule而不是DOM元素是一种模糊但完全有效(和标准化 )的方式来改变元素的风格。 It's obscure because it is difficult, requiring a nested loop going through all the rules in all the stylesheets to find the rule you want to change. 它很难模糊,因为它很难,需要一个嵌套循环遍历所有样式表中的所有规则,以找到您想要更改的规则。 And it's obscure also because it's not super valuable - you can usually accomplish the same thing by just accessing the DOM element's style property. 它也很模糊,因为它不具有超级价值 - 你通常可以通过访问DOM元素的style属性来完成同样的事情。

But, with pseudo-elements, there is no DOM element. 但是,使用伪元素,没有DOM元素。 The pseudo-element is a product of the style rule, so the only way to manipulate the pseudo-element is through the style rule. 伪元素是样式规则的产物,因此操作伪元素的唯一方法是通过样式规则。 People recommend adding style rules because that is easier than finding the style rule and editing it. 人们建议添加样式规则,因为这比查找样式规则和编辑它更容易。 But finding and editing it is perfectly valid. 但查找和编辑它是完全有效的。

You can have the best of both worlds by adding a style rule once, and then keeping a reference to the rule and making subsequent edits to that same rule. 您可以通过添加样式规则一次,然后保留对规则的引用并对该规则进行后续编辑,从而实现两全​​其美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM