简体   繁体   English

这个跨度颜色不应该是绿色吗?

[英]Shouldn't this span color be green?

Shouldn't the span color should be green, it is red in Chrome.跨度颜色不应该是绿色的,它在 Chrome 中是红色的。

HTML spec says it should be green, it is green in Firefox. HTML 规范说它应该是绿色的,它在 Firefox 中是绿色的。

The HTML spec uses fetch to get the resources referenced by elements (see this and fetch is always an async operation. So the computed style is gotten before the new stylesheet has loaded, and hence the color should be green. HTML 规范使用 fetch 来获取元素引用的资源(请参阅此内容并且 fetch 始终是异步操作。因此在加载新样式表之前获取计算样式,因此颜色应为绿色。

 var div = document.createElement("div"); document.body.appendChild(div); var link = document.createElement("link"); link.href = "data:text/css,div { color: red }"; link.rel = "stylesheet"; var div = document.createElement("div"); document.body.appendChild(div); var link = document.createElement("link"); link.href = "data:text/css,div { color: red }"; link.rel = "stylesheet"; document.head.appendChild(link); document.querySelector("span").style.color = getComputedStyle(div).color; link.remove(); div.remove();
 div { color: green }
 <span>This should be green</span>

To make this easier to analyze, I've reduced your sample to remove the duplicate and irrelevant lines;为了使分析更容易,我减少了您的样本以删除重复和不相关的行; the following is enough to reproduce the issue:以下内容足以重现该问题:

 var div = document.createElement("div"); document.body.appendChild(div); var link = document.createElement("link"); link.href = "data:text/css,div { color: red }"; link.rel = "stylesheet"; document.head.appendChild(link); document.querySelector("span").style.color = getComputedStyle(div).color;
 div {color:green}
 <span>This is a span</span> <div>this is a div</div>

So what's going on here is, there's inline CSS that says divs should be green;所以这里发生的事情是,有内联 CSS 说 div 应该是绿色的; you generate a text/css link that sets divs to red, and append that to the document head.您生成一个将 div 设置为红色的 text/css 链接,并将其附加到文档头。 Then you use getComputedStyle to copy the color from the div onto the span.然后使用getComputedStyle将颜色从 div 复制到跨度上。

In Safari, Chrome, and Edge, both lines end up red;在 Safari、Chrome 和 Edge 中,两条线都以红色结束; in Firefox the span is green and the div is red.在 Firefox 中,跨度为绿色,div 为红色。

BUT!但是! on first load, or if you load this page in Safari or Chrome with a clean, empty cache * you'll see the same behavior as in Firefox: green span and red div.首次加载时,或者如果您在 Safari 或 Chrome 中 使用干净的空缓存加载此页面*您将看到与 Firefox 中相同的行为:绿色跨度和红色 div。 Edge never behaves like FF, it's always red for both even on first load. Edge 的行为永远不会像 FF,即使在第一次加载时,它也始终为红色。

* (In Safari you can just use a private browsing window. I could swear I was seeing the same in Chrome at one point but am no longer able to reproduce; possibly I was mistaken.) * (在 Safari 中,您可以只使用隐私浏览窗口。我可以发誓,我曾在 Chrome 中看到过相同的内容,但不再能够重现;可能我弄错了。)

Therefore, here's my hypothesis for what's going on:因此,这是我对正在发生的事情的假设:

  • In Safari, when the generated stylesheet link has never been used before, it's async, so getComputedStyle picks up the color from the inline style.在 Safari 中,当生成的样式表链接以前从未使用getComputedStyle ,它是异步的,因此getComputedStyle从内联样式中选取颜色。 On later loads, the generated stylesheet is already in cache when getComputedStyle runs, so its rule takes over.在以后的加载中,当getComputedStyle运行时,生成的样式表已经在缓存中,因此它的规则会接管。
  • In Firefox, stylesheet links are always treated as async, so getComputedStyle always picks up the inline rule.在 Firefox 中,样式表链接始终被视为异步,因此 getComputedStyle 始终选择内联规则。
  • In other browsers, the stylesheet link is interpreted synchronously (possibly because the browser can tell that a data URI doesn't need network time?) so is handled before getComputedStyle runs.在其他浏览器中,样式表链接是同步解释的(可能是因为浏览器可以判断data URI 不需要网络时间?)因此在getComputedStyle运行之前进行处理。

This is a weird enough edge case that I'm not certain which behavior could be described as "according to spec".这是一个足够奇怪的边缘情况,我不确定哪种行为可以被描述为“根据规范”。

尝试 HTML 或 HTML5(这是 Chrome):

<span type="color:green">shouldn't this be green</span>

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

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