【问题标题】:Focus input on button click using bootstrap使用引导程序将输入重点放在按钮单击上
【发布时间】:2021-03-02 05:33:15
【问题描述】:

Bootstrap 4 主要折叠示例代码。 https://getbootstrap.com/docs/4.0/components/collapse/

<p>
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    Button
  </button>
</p>
<div class="collapse" id="collapseExample">
  <div class="card card-body">
    <input type="text" name="test" id="test-id">
  </div>
</div>

当您单击按钮时,有一个带有输入字段的隐藏 div,当有人单击按钮时,我如何才能集中输入?使用 JS onlick 似乎会中断引导程序的崩溃能力。

【问题讨论】:

    标签: javascript html twitter-bootstrap bootstrap-4


    【解决方案1】:

    确保在bootstrap.jsbootstrap.css 之前添加了jQuery(最好是3.5.1)库,并在setTimeout() 函数中使用它的focus() 函数。这将在单击按钮一定毫秒数后关注input

    这是一个显示它工作的小提琴(1秒后聚焦):

    https://jsfiddle.net/bradberkobien/z5wr96Lt/18/

    【讨论】:

    • 完美运行!
    • @RicardoMehr 太棒了,很高兴为您提供帮助。您可能可以将其降低到半秒(500 而不是1000)。这可能对用户有更多帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2019-07-25
    • 2017-09-18
    • 1970-01-01
    • 2014-11-10
    相关资源
    最近更新 更多