2 回答

TA貢獻1852條經驗 獲得超1個贊
最簡單的方法如下
$('#searchbar').on('input keydown', function (e) {
if ($('#searchbar').val().length >= 3) {
$('.child-div').show();
}
if( e.which == 13){
$('.child-div').hide();
e.target.blur();
}
})
$('#searchbar').on('focus', function (e) {
if ($('#searchbar').val().length >= 3) {
$('.child-div').show();
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<input type="text" class="form-control form-rounded rounded-pill" placeholder="Text input" id="searchbar">
<div class="child-div" style="background-color: blueviolet; width: 50%; height: 200px; display: none;">
CHILD DIV
</div>
</div>
在這里,您只需在按下回車鍵后執行一個模糊事件,并注冊焦點事件,它會檢查是否應該打開子 div

TA貢獻1831條經驗 獲得超4個贊
您可以使用 JQuery blur() 和 focus() 事件。當用戶按下回車鍵時,我們將以編程方式關閉框并觸發輸入 blur() 事件以使其取消焦點。然后注冊一個 focus() 事件,只要輸入是焦點,它就會顯示框。
添加回答
舉報