【问题标题】::before :after not working on the input type submit? [duplicate]:before :after 不处理输入类型提交? [复制]
【发布时间】:2014-08-13 11:21:06
【问题描述】:

为什么 :before 和 :after 不能处理 input type='submit'

这是我的代码

input.submit:before {     
    display: inline-block;
    -webkit-font-smoothing: antialiased;
    font: normal 18px/1 'Genericons';
    content: '\f400';
}

【问题讨论】:

  • 你能不能把 HTML 也显示出来。也许可以做一个 Fiddle 来显示。

标签: html css


【解决方案1】:

这是因为:before:after 仅适用于可以有子节点的节点。在您的情况下,使用按钮会起作用,因为它可以采用 HTML。


5.12.3 The :before and :after pseudo-elements

':before' 和 ':after' 伪元素可用于在元素内容之前或之后插入生成的内容。

由于输入不能有内容,所以不支持

【讨论】:

  • 解释了很多。不过,我还没有在其他地方找到这个答案。
  • @Juan Mendes 我发现您的回答比原来的“重复”更有帮助。感谢您的澄清!
  • @webprogrammer 特别是当这个答案适合 IE7 并建议使用 jQuery 时。
【解决方案2】:

试试这个,在你的代码中你的目标按钮是submit,但是你应该像下面这样使用submit类型的按钮

 input[type="submit"]:before {
         display: inline-block;
         -webkit-font-smoothing: antialiased;
         font: normal 18px/1 'Genericons';
         content: '\f400'; 
    }

【讨论】:

    【解决方案3】:

    使用<button>

    Have a fiddle!

    HTML

    <button>Submit</button>
    

    CSS

    button:before {    
    display: inline-block;
        height: 20px; 
        background: #F00;
         -webkit-font-smoothing: antialiased;
         font: normal 18px/1 'Genericons';
        content: '\f400';
    }
    

    【讨论】:

    • 这不允许提交表单,它是一个 HTML 元素但它不是输入。
    • @iLen - 不确定你的意思。见stackoverflow.com/a/469077/2930477
    • @iLen The type of the button. Possible values are: submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value. MDN button
    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 2021-08-13
    • 2011-06-17
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    相关资源
    最近更新 更多