【问题标题】:Targeting IE6/7/8 with .lt-ie9 class — not working with IE8使用 .lt-ie9 类针对 IE6/7/8 — 不适用于 IE8
【发布时间】:2012-03-11 08:42:15
【问题描述】:

我有一个form,它在每个字段中使用各种 PNG 图像,根据填写进度以及该字段是否不是验证所需的字段,使用伪类切换。

几乎不存在的 IE 对伪类的支持不会破坏交易,前提是我可以在所有浏览器中显示基本图标——尽管无需切换以反映 IE6/7/8 中字段状态的变化。

但是,我对以下图片的 CSS 有疑问:

#contact input#url:empty,.lt-ie9 #contact input#url{background:url(url-icon.png) no-repeat left center;margin-bottom:24px}

基本上,仅使用#contact input#url:empty 选择器,当页面在 IE6-8 中加载时它根本不会显示。通过添加.lt-ie9 #contact input#url 选择器,它可以在 IE6 和 IE7 中正确显示但由于某种原因在 IE8 中无法显示

这是 HTML(CSS 可在此处获得:http://i.via.dj/EOma):

<div id="contact-form">

<form id="contact" class="clear" method="post" accept-charset="utf-8" novalidate="novalidate">

<fieldset>

<input type="text" name="checking" placeholder="yourname@domain.com" class="hidden" id="checking">
<label for="checking" class="hidden">If you do not want to submit this form, enter your email in this field</label>

<p class="name">
<input type="text" name="name" placeholder="John Doe" title="Enter your name" required="required" id="name">
<label for="name">Name *</label>
<label class="error" for="name" id="name-error">Name is required</label>
</p>

<p class="email">
<input type="email" name="email" placeholder="yourname@domain.com" autocapitalize="off" title="Enter your e-mail address" required="required" id="email">
<label for="email">Email *</label>
<label class="error" for="email" id="email-error">Type a valid email</label>
</p>

<p class="website">
<input type="url" name="url" placeholder="http://" autocapitalize="off" id="url">
<label for="url">Website</label>
</p>

<p class="message">
<textarea name="message" placeholder="Type your message here (max. length 1,200 characters)" title="Enter your comment or message" required="required" id="message" maxlength="1200"></textarea><label for="message">Message *</label>
<label class="error" for="message" id="message-error">You haven't typed a message</label>
</p>

<p class="submit">
<input type="submit" value="Send message" />
</p>

</fieldset>

</form>

非常感谢任何建议。

【问题讨论】:

    标签: css internet-explorer internet-explorer-8 pseudo-class


    【解决方案1】:

    这里的选择器不会像你期望的那样表现:

    #contact input#url:empty,.lt-ie9 #contact input#url{background:url(url-icon.png) no-repeat left center;margin-bottom:24px}
    

    :empty 在低于 9 的 IE 中不受支持,因此上述整个规则将(或应该..)在所有低于 9 的 IE 版本中被完全忽略。

    我不确定为什么它在 IE7 中仍然适用于您。 IE7 太疯狂了。

    来自the spec

    当用户代理无法解析选择器时(即,它不是有效的 CSS 2.1),它必须忽略选择器和下面的声明块(如果有的话)。

    要解决您的问题,请尝试将其拆分为两条规则:

    #contact input#url:empty{background:url(url-icon.png) no-repeat left center;margin-bottom:24px}
    .lt-ie9 #contact input#url{background:url(url-icon.png) no-repeat left center;margin-bottom:24px}
    

    【讨论】:

    • 感谢您的回复。但是我的CSS实际上不是两个规则:一个带有选择器#contact input#url:empty(就像你说的那样,9以下的IE不支持,因此如果单独包含则显示空白);第二个是选择器.lt-ie9 #contact input#url,它取消了:empty,应该触发在IE 6、7 8中显示图像?
    • 没有。这是一条规则:#contact input#url:empty,.lt-ie9 #contact input#url{background:url(url-icon.png) no-repeat left center;margin-bottom:24px}。 IE8 无法解析选择器#contact input#url:empty,.lt-ie9 #contact input#url,因此它会丢弃整个规则。 IE6/7 应该做同样的事情。
    • 再想一想,input:empty 可能也不是你想要的。 :empty matches elements that have no children. 因此input:emptyalways going to match
    • 啊,对。我想我可以通过组合它来节省那个已经很大的 css 文件中的一些空间。我尝试了您的修复,它解决了 IE8 中的问题。不过奇怪的是,IE6 和 7 肯定是在阅读规则的“组合”版本……谢谢。
    • IE7 并不完全遵守忽略它无法解析的规则的规则。当 IE7 发现它无法识别的伪类选择器时(例如 :empty),它只是假装该类不存在。 IE8 停止这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    相关资源
    最近更新 更多