【问题标题】:&:focus on input wont change element&:focus on input 不会改变元素
【发布时间】:2020-09-13 08:31:53
【问题描述】:

我正在尝试在输入我的input 标签后显示一个按钮。目前我将我的按钮设置为display: none,当我专注于输入字段时,它应该改变我的display: inline-block,这应该让它重新出现。我做错了什么?

html

            <h1 className='title-caption'>Get started today!</h1>
                <div className='email-newsletter-container'>
                    <p className='sign-up-caption'>
                        Sign up for our newletter for more information
                    </p>
                    <input
                        className='email-input'
                        placeholder='Enter email address'
                    ></input>
                    <br />
                    <button className='submit-button' id='submit-button'>
                        Submit
                    </button>
                </div>

css

    .email-input {
        margin-top: 20px;
        height: 40px;
        width: 400px;
        text-align: center;
        border-radius: 5px;
        border: none;
        outline: none;
        position: relative;

        &:focus + .submit-button {
            display: inline-block;
        }
    }

    .email-input:focus + .submit-button {
        display: inline-block;
    }

    .submit-button {
        position: relative;
        border-radius: 20px;
        width: 90px;
        margin-top: 15px;
        padding: 10px 10px 10px 10px;
        font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande',
            'Lucida Sans', Arial, sans-serif;
        background-color: transparent;
        border: 2px solid white;
        color: white;
        display: none;
    }

【问题讨论】:

  • 只是为了确保:您的“CSS”代码必须首先通过预处理器运行。你做到了,对吧?
  • @Sirko,确定他没有,这些都是 CSS 选择器
  • 尝试使用~ 选择器而不是+ 选择器。 + 将选择任何紧随其后的元素,而 ~ 选择任何紧随其后的元素
  • @Simplicius 您能否向我解释一下这适用于 CSS 标准的哪一部分:&amp;:focus + .submit-button 作为嵌套选择器。
  • @Sirko,这里有一些参考:w3schools.com/cssref/css_selectors.asp

标签: html css focus


【解决方案1】:

button {
  display: none;
}

input:focus ~ button {
  display: inline-block;
}
<input type="text">
<div></div>
<button>Button</button>

使用~ 选择器代替+ 选择器。 Here 的小示范。

+ 选择器只会选择紧随其后的同级,而 ~ 选择器将选择放置在其后的任何同级,然后我们可以使用类选择器过滤它们。

【讨论】:

    【解决方案2】:

    如果你尝试这样的事情会怎样 Demo

    CSS

    .email-input {
        margin-top: 20px;
        height: 40px;
        width: 400px;
        text-align: center;
        border-radius: 5px;
        border: none;
        outline: none;
        position: relative;
    
        &:focus + .submit-button {
            display: inline-block;
        }
    }
    
    .email-input:focus + .submit-button {
        display: inline-block;
    }
    
    .submit-button {
        position: relative;
        border-radius: 20px;
        width: 90px;
        margin-top: 15px;
        padding: 10px 10px 10px 10px;
        font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande',
            'Lucida Sans', Arial, sans-serif;
        background-color: transparent;
        border: 2px solid white;
        color: white;
        display: none;
    }
    

    HTML

     <script>
     function myFunction() {
     var x = document.getElementById("myDIV");
     if (x.style.display === "block") {
     x.style.display = "none";
     } else {
     x.style.display = "block";
     }
     }
    </script>
    <h1 className='title-caption'>Get started today!</h1>
                <div className='email-newsletter-container'>
                    <p className='sign-up-caption'>
                        Sign up for our newletter for more information
                    </p>
                    <input
                        className='email-input'
                        placeholder='Enter email address'
                        onfocus="myFunction()"
                        
                    ></input>
                    <br />
                    <button style="display:none" className='submit' id="myDIV">
                        Submit
                    </button>
                </div>
                
    

    【讨论】:

      【解决方案3】:
      • &lt;input /&gt; 标签是自封闭的。你的语法不正确。

      • &amp;:focus + .submit-button { display: inline-block; } - 这意味着在输入后跟随 .submit-buton 但在你的 html 中输入后有&lt;br/&gt; 标签,所以这不起作用

      【讨论】:

      • 好吧,我写的一般都是关于错误的,那是不是很糟糕?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 2016-05-21
      • 2017-06-21
      • 2018-08-12
      相关资源
      最近更新 更多