
本文介绍如何使用 jQuery 实现当多个输入框中的任何一个获得焦点时,始终保持一个可折叠的 div 展开,并在失去焦点时关闭该 div。核心思路是利用 focus 和 focusout 事件,配合 collapse 方法控制 div 的显示与隐藏。
Bootstrap 的 Collapse 组件允许您通过点击或其他方式切换内容的可见性。 默认情况下,点击带有 data-toggle="collapse" 和 data-target="#yourCollapseID" 属性的元素会切换与 #yourCollapseID 关联的 div 的可见性。 然而,如果希望根据输入框的焦点状态来控制 Collapse 组件,则需要使用 JavaScript (特别是 jQuery) 来监听 focus 和 focusout 事件,并相应地调用 collapse('show') 和 collapse('hide') 方法。
HTML 结构: 确保您的 HTML 结构包含触发器输入框和一个可折叠的 div。 每个输入框都应具有一个共同的 class,以便于 jQuery 选择器使用。
<td>
<input
type="text" name="tbtext1" class="toggleDepDiv" style="width: 90%;"
data-toggle="collapse" data-target="#exampleModal"
aria-expanded="false" aria-controls="exampleModal" />
</td>
<td style="width:20%">
<input type="text" name="tbtext1" class="toggleDepDiv" style=" width: 85%; position: relative; right: 0.9rem; "
data-toggle="collapse" data-target="#exampleModal" aria-expanded="false" aria-controls="exampleModal" />
</td>
<td style="width:20%">
<input type="text" name="tbtext1" class="toggleDepDiv" style="width: 78%; position: relative; right: 2.3rem;"
data-toggle="collapse" data-target="#exampleModal" aria-expanded="false" aria-controls="exampleModal" /><span style="position: relative; right: 1.2rem; ">= $</span>
</td>
<td style="width:20%">
<input type="text" name="tbtext1" style="width: 99%; position: relative; right: 0.8rem; " />
</td>
<div class="collapse" id="exampleModal">
<div class="row" style="margin-top:1rem">
<div class="col-md-12">
<h5 class="modal-title" id="exampleModalLabel">Calc phys% using effective age of
<input type="text" id="phys" style="width:3rem" />/Lifespan of <input type="text" id="phys" style="width:3rem" />
</h5>
</div>
</div>
<div class="modal-body">
<table class="innerModalTable" style="width: 100%; text-align: center; border-top: none; margin-left: -1.7rem;">
<tr>
<th class="borderTd " style="text-align:center;border-top:none;border-left:none"> </th>
<th class="borderTd " style="text-align: left; width: 9.3rem; border-top: none; position: relative; left: 2rem;">Physical</th>
<th class="borderTd " style="text-align: center; border-top: none; width: 9rem;">External</th>
<th class="borderTd " style="text-align: center; border-top: none; border-right: none; width: 8rem;">Functional</th>
<th> </th>
</tr>
<tr>
<td class="borderTd ">% of Cost New</td>
<td class="borderTd "><input type="text" style="width:100%" class="noBorderInputFocusOutline" /></td>
<td class="borderTd "><input type="text" style="width: 100%" class="noBorderInputFocusOutline" /></td>
<td class="borderTd "><input type="text" style="width: 100%" class="noBorderInputFocusOutline" /></td>
<td> </td>
</tr>
<tr>
<td class="borderTd " style="width: 10.3rem; text-align: left;">Lump Sum</td>
<td class="borderTd "><input type="text" style="width: 100%" class="noBorderInputFocusOutline" /></td>
<td class="borderTd "><input type="text" style="width: 100%" class="noBorderInputFocusOutline" /></td>
<td class="borderTd "><input type="text" style="width: 100%" class="noBorderInputFocusOutline" /></td>
<td> </td>
</tr>
</table>
</div>
</div>jQuery 代码: 使用 jQuery 监听输入框的 focus 和 focusout 事件。
$(function () {
$('.toggleDepDiv').on('focus', function () {
$('#exampleModal').collapse('show');
});
$('.toggleDepDiv').focusout(function (e) {
// 检查焦点是否转移到另一个 .toggleDepDiv 元素
if (!$(e.relatedTarget).hasClass('toggleDepDiv')) {
$('#exampleModal').collapse('hide');
}
});
});通过使用 jQuery 监听 focus 和 focusout 事件,并结合 Bootstrap Collapse 组件的 show 和 hide 方法,可以实现根据输入框的焦点状态动态控制 Collapse 组件的显示和隐藏。 这种方法可以提高用户体验,并使表单更加易于使用。
以上就是保持可折叠Div在输入框获得焦点时始终展开的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号