簡體   English   中英

如何將 select 框與 jQuery 而不是 select 框內的形式?

[英]How to select a box with jQuery but not select the form inside the box?

我有一個代碼,我希望在使用“框搜索”單擊框時關閉頁面 class 但是即使我說不考慮表單和表單內的元素,表單也會在表單時關閉和輸入被點擊。

 $(".box-search *").not('form, form *').click(function() { $(".box-search").fadeOut(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="box-search"> <div class="container" style="font-size: 40px; color: red;"> <button type="submit" class="close-box-search">Close</button> <form method="get" action=""> <input type="text"> <button type="submit">Search</button> </form> </div> </div>

我找到了問題的答案 To select the parent, but not apply to the child 我們使用下面的代碼

$(".box-search").click(function (e) { 
    if( $(e.target).hasClass("box-search") || $(e.target).hasClass("container") || $(e.target).hasClass("close-box-search") ){ 
        hideBoxSearch()
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM