gpt4 book ai didi

javascript - 捕获 .bs.dropdown 事件

转载 作者:行者123 更新时间:2023-12-02 23:46:37 28 4
gpt4 key购买 nike

我正在尝试从引导下拉列表中捕获事件。似乎什么都不起作用,尽管显然我做错了什么。

代码,精简为问题......

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<title>Demo</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.js" integrity="sha256-DYZMCC8HTC+QDr5QNaIcfR7VSPtcISykd+6eSmBW5qo=" crossorigin="anonymous"></script>
</head>

<body>
<div class="navbar">
<a class="nav-link dropdown-toggle" href="#" id="search_drop" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Search
</a>

<div class="dropdown-menu" aria-labelledby="search_drop">
<h2 class="text-primary">Search</h2>
<form class="navbar-form navbar-right navbar-expand-xl" id="header-searchForm">
<input type="text" class="form-control" placeholder="enter terms" name="Search" aria aria-selected="true" id="search_str" required="true">
<button type="submit" class="btn btn-sm btn-primary">
Search
</button>
</form>
</div>
</div>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js" integrity="sha384-xrRywqdh3PHs8keKZN+8zzc5TX0GRTLCcmivcbNJWm2rs5C8PRhcEn3czEjhAO9o" crossorigin="anonymous"></script>
<script type="text/javascript">
$('#search_drop').on('show.bs.dropdown', function() {
console.log("show");
}).on('shown.bs.dropdown', function() {
console.log("shown");
}).on('hide.bs.dropdown', function() {
console.log("hide");
}).on('hidden.bs.dropdown', function() {
console.log("hidden");
});
</script>

</body>
</html>

没有任何一个 console.log 语句被命中。

最佳答案

您需要将触发按钮包装在具有下拉类和目标的元素中:

All dropdown events are fired at the .dropdown-menu’s parent element and have a relatedTarget property, whose value is the toggling anchor element.

还有...

Wrap the dropdown’s toggle (your button or link) and the dropdown menu within .dropdown, or another element that declares position: relative;.

$('.dropdown').on('show.bs.dropdown', function() {
console.log("show");
}).on('shown.bs.dropdown', function() {
console.log("shown");
}).on('hide.bs.dropdown', function() {
console.log("hide");
}).on('hidden.bs.dropdown', function() {
console.log("hidden");
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"><script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="navbar">
<div class="dropdown">
<a class="nav-link dropdown-toggle" href="#" id="search_drop" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Search
</a>

<div class="dropdown-menu" aria-labelledby="search_drop">
<h2 class="text-primary">Search</h2>
<form class="navbar-form navbar-right navbar-expand-xl" id="header-searchForm">
<input type="text" class="form-control" placeholder="enter terms" name="Search" aria aria-selected="true" id="search_str" required="true">
<button type="submit" class="btn btn-sm btn-primary">
Search
</button>
</form>
</div>
</div>
</div>

关于javascript - 捕获 .bs.dropdown 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55834376/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com