简体   繁体   English

Chrome和CSS属性选择器

[英]Chrome and CSS attribute selector

I have the following HTML code, where I would like to format with css a data format (coming from xml) which can't be changed. 我有以下HTML代码,我想用css格式化一种无法更改的数据格式(来自xml)。

I have to give different styles to elements with different attribute value. 我必须为具有不同属性值的元素提供不同的样式。 I thought to use the CSS attribute selector. 我想使用CSS属性选择器。

body {
    background-color: black
}
s {
    text-decoration: none
}
f {
    color: black;
    text-decoration: none;
    display: block;
}
f[type=h2] {
    color: green
}
f[type=p] {
    color: blue
}
f[type=speech] {
    color: yellow
}
f[type=other] {
    color: gray
}
<b>
  <s> 
    <f type="h2">Title</f>
    <f type="p">Paragraph</f>
    <f type="speech">Dialgoue</f>
    <f type="other" br="true">Other</f>
    <f type="p">Paragraph</f>
    <f type="p">Paragraph</f>
  </s>
</b>

In Firefox the page is rendered as I expect ( h2 in green, p in blue, speech in yelllow and other in gray). 在Firefox中,页面按照我的预期呈现( h2为绿色, p为蓝色, speech为yelllow,其他为灰色)。 In chrome everything is green. 在chrome中,一切都是绿色的。

How can I obtain the Firefox result in Chrome? 如何在Chrome中获取Firefox结果?

Changing attr name will help you , andi have no idea why 更改attr名称将对您有所帮助,而且我不知道为什么

        <f custom="p">Paragraph</f>
        <f custom="speech">Dialgoue</f>
        <f custom="other" br="true">Other</f>
        <f custom="p">Paragraph</f>
        <f custom="p">Paragraph</f>


   <style type="text/css">
        f[custom=h2] {
            color: green;
        }
        f[custom=p] {
            color: blue;
        }
        f[custom=speech] {
            color: yellow;
        }
        f[custom=other] {
            color: gray;
        }
    </style>

Your HTML is not valid. 您的HTML无效。 If you run it through a validator it spits out errors because it doesn't like the named embedded XML tags. 如果通过验证器运行它,则会发出错误,因为它不喜欢命名的嵌入式XML标记。

According to the HTML/XML Task Force Report : 根据HTML / XML特别工作组报告

When an HTML5 parser encounters unfamiliar markup, it assumes that such markup is an erroneous attempt to generate well-defined HTML5. 当HTML5解析器遇到不熟悉的标记时,它会假定这样的标记是生成定义良好的HTML5的错误尝试。 Consequently, it applies error correction strategies which result in a DOM representation that can differ radically from the DOM that an XML parser would have produced. 因此,它应用了纠错策略,这些策略导致DOM表示与XML解析器可能产生的DOM完全不同。 In particular, open elements may end prematurely and additional elements may be opened. 特别地,开放元件可能过早地结束并且可以打开附加元件。

The practical result is that a “naked” XML island in an HTML5 document will not reliably produce anything that resembles the DOM one would expect from casual inspection of the XML island. 实际结果是,HTML5文档中的“裸”XML岛将无法可靠地生成任何类似于人们对随后检查XML岛所期望的DOM。

So Chrome is well within its rights to screw up here, 'cause technically you did it first. 所以Chrome完全有权在这里搞砸,因为从技术上讲,你首先要做到这一点。 Of note is how (in my Chrome browser) all the elements are green ( http://jsfiddle.net/qJMWg/ ) - which suggests that for some reason that all thing they're nested in a big <f type="h2"> element. 值得注意的是(在我的Chrome浏览器中) 所有元素都是绿色的http://jsfiddle.net/qJMWg/ ) - 这表明由于某些原因它们所有的东西都嵌套在一个大的<f type="h2">元素。 That is of note because HTML does contains a <b> and <s> tag, so <f> is the first invalid one it encounters. 这是值得注意的,因为HTML确实包含<b><s>标记,因此<f>是它遇到的第一个无效标记。

If we change the styles for that f\\[type=h2\\] rule ( http://jsfiddle.net/qJMWg/1/ ) it affects everything - which is consistent with the idea that somehow Chrome is interpreting this XML structure incorrectly. 如果我们更改f\\[type=h2\\]规则http://jsfiddle.net/qJMWg/1/的样式,它会影响所有内容 - 这与Chrome不正确地解释此XML结构的想法是一致的。 To Chrome's CSS engine (despite what the developer tools is telling us) this somehow looks like this: 对于Chrome的CSS引擎(尽管开发人员工具告诉我们),这看起来像这样:

    <b>
      <s> 
        <f type="h2">Title&lt;/f&gt;
        &lt;f type="p"&gt;Paragraph&lt;/f&gt;
        &lt;f type="speech"&gt;Dialgoue&lt;/f&gt;
        &lt;f type="other" br="true"&gt;Other&lt;/f&gt;
        &lt;f type="p"&gt;Paragraph&lt;/f&gt;
        &lt;f type="p"&gt;Paragraph</f>
      </s>
    </b>

For some unknown reason, Chrome is rather strict on HTML tags thus the CSS rule won't work well in the said browser. 由于某些未知原因,Chrome对HTML标记非常严格,因此CSS规则在上述浏览器中无法正常工作。

A suggestion though, why don't you style the XML instead? 但是有一个建议,为什么不改变XML的样式呢?

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

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