【问题标题】:Show element with input button only with css仅使用 css 显示带有输入按钮的元素
【发布时间】:2021-01-30 23:36:27
【问题描述】:

我想在选中另一个部分的复选框时显示一个部分,并从顶部显示动画。对于另一部分中的输入,我有以下代码。

<div className="continue" id="first">
  <button className="btn-continue">
                    Contratar Plano
                    <input type="checkbox" id="reveal-email" role="button"/>
                  </button>

</div>

<section className="plan-section" id="plan-section">
  <div className="next">
    <i class="arrow down"></i>
  </div>
  <div className="form-block">
    <form className="form">
      <div className="plan-form">
        <div className="input-block">
          <label htmlFor="name">Nome</label>
          <input type="text" name="name" id="name" onChange={props.handleChange} required className="input" />
        </div>

        <div className="continue">
          <button className="btn-continue" id="plan-continue" disabled={props.step.isLast()} onClick={props.next}>
                  <span className="btn-text">Contratar Plano</span>
                  <img className="check-btn" src={check} />
                </button>
        </div>
      </div>
    </form>
  </div>
</section>

还显示了我需要显示的部分;这个部分有一个默认的display:none

【问题讨论】:

  • 为什么只使用CSS,根据你的描述这是classicJS任务?
  • @Sergey-N13 我认为只使用css会更简单,但也接受js
  • 注意&lt;input&gt;(或&lt;input /&gt;)是一个自闭合标签,没有对应的&lt;/input&gt;标签。
  • @DavidsaysreinstateMonica 是的,我在代码上修复了这个问题
  • &lt;button class=""btn-continue&gt;&lt;button&gt;你想点击显示下一个&lt;section&gt;吗? &lt;button&gt; 是在它自己的父级 &lt;section&gt; 中,还是只是包裹在 &lt;div&gt; 中,这是您的代码片段中 &lt;section&gt; 的兄弟?

标签: html css reactjs forms input


【解决方案1】:

这是一个经典的 JS 任务。使用 onclick 事件检查复选框是否被选中,然后将部分从 display: none 更改为 display: block 。还要添加一个onclick事件,以便触发JS。

function showSection() {
  var showSection = document.getElementById("reveal-email"),
      planSection = document.getElementById("plan-section");
  if (showSection.checked == true) {
    planSection.style.display = "block";
  } else {
    planSection.style.display = "none";
  }
}
#plan-section {
  display: none;
}
<div className="continue" id="first">
  <button className="btn-continue">Contratar Plano
    <input type="checkbox" id="reveal-email" onclick="showSection()">
  </button>

</div>

<section className="plan-section" id="plan-section">
  <div className="next">
    <i class="arrow down"></i>
  </div>
  <div className="form-block">
    <form className="form">
      <div className="plan-form">
        <div className="input-block">
          <label htmlFor="name">Nome</label>
          <input type="text" name="name" id="name" onChange={props.handleChange} required className="input" />
        </div>

        <div className="continue">
          <button className="btn-continue" id="plan-continue" disabled={props.step.isLast()} onClick={props.next}>
                  <span className="btn-text">Contratar Plano</span>
                  <img className="check-btn" src={check} />
                </button>
        </div>
      </div>
    </form>
  </div>
</section>

【讨论】:

  • @tacshy 我尝试了您的解决方案但不起作用,我正在使用反应中的多步并有一个 4 步表单,当我单击输入时没有任何反应
【解决方案2】:

关于您的代码的一项建议:

<button className="btn-continue">
                    Contratar Plano
    <input type="checkbox" id="reveal-email" role="button"></input>
</button>

将复选框和一些文本分组到按钮中不是一个好习惯,在 HTML 中您可以使用 label

如果JS方案可以接受,则需要按照以下步骤进行:

  1. Find DOM 中的复选框按钮和部分
  2. Add事件监听器,每次复选框状态改变后触发回调函数
  3. 在回调函数中,您需要为您的部分触发style

完整代码如下:

var checkbox = document.getElementById('reveal-email');
var section = document.getElementById('plan-section');

checkbox.addEventListener('change', onChange)

function onChange() {
  if (this.checked) {
    section.style.display = "block";
  } else {
    section.style.display = "none";
  }
}
<div className="continue" id="first">
  <button className="btn-continue">
    Contratar Plano
    <input type="checkbox" id="reveal-email" role="button"/>
  </button>
</div>

<section className="plan-section" id="plan-section" style="display:none">
  <div className="next">
    <i class="arrow down"></i>
  </div>
  <div className="form-block">
    <form className="form">
      <div className="plan-form">
        <div className="input-block">
          <label htmlFor="name">Nome</label>
          <input type="text" name="name" id="name" onChange={props.handleChange} required className="input" />
        </div>

        <div className="continue">
          <button className="btn-continue" id="plan-continue" disabled={props.step.isLast()} onClick={props.next}>
            <span className="btn-text">Contratar Plano</span>
            <img className="check-btn" src={check} />
          </button>
        </div>
      </div>
    </form>
  </div>
</section>

【讨论】:

  • 即使您的 anwser 工作正常,也请尽力解释它工作的原因或 OP 需要做什么。提供代码是一回事,让他们了解问题和解决方案是要走的路。
  • @tacoshy 是的,我同意你的观点,解释比回答更可取。我已经更新了我的答案,并且我想建议您在下一个答案中避免在 HTML 中使用 JS,因为许多新手开发人员都可以将其视为最佳实践。
  • @Sergey-N13 我尝试了您的代码,但收到错误“TypeError: Cannot read property 'addEventListener' of null”
  • @PaolaSantos 您是否将此代码粘贴到页面的顶部或底部?如果您在页面顶部粘贴 JS 代码,则需要将此代码包装到窗口加载事件中。 developer.mozilla.org/en-US/docs/Web/API/Window/load_event
  • @Sergey-N13 我修复了这个问题,但仍然无法正常工作,该部分仍然没有出现
猜你喜欢
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
相关资源
最近更新 更多