admin管理员组文章数量:1406943
I'm using the basic Twitter Bootstrap navbar for my menu, and I now want to show notifications in a dropdown inspired by the system here on Stackoverflow. To acplish this I want to call the api when clicking the dropdown, for which I need to "catch" the dropdown event. According to the docs, I need to catch the event like so:
$('#myDropdown').on('show.bs.dropdown', function () {
// do something…
})
So I've got a working fiddle here, but when I use the exact same code in my own html (see below) it doesn't work anymore. Does anybody know what's wrong with the code below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!-- jQuery -->
<script src=".11.1.min.js"></script>
<!-- Bootstrap -->
<link href=".3.2/css/bootstrap.min.css" rel="stylesheet">
<script src=".3.2/js/bootstrap.min.js"></script>
<script type="text/javascript">
$('#jajaja').on('show.bs.dropdown', function () {
alert('Come on lets show the dropdown!!');
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/">My Website</a>
</div>
<div id="navbar" class="navbar-collapse">
<ul class="nav navbar-nav navbar-right" id="jajaja">
<li class="dropdown">
<a href="" id="notifications-header" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Notifications <span class="caret"></span></a>
<ul id="notifications" class="dropdown-menu" role="menu">
<li><span class="glyphicon glyphicon-refresh text-center" aria-hidden="true"></span></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
I'm using the basic Twitter Bootstrap navbar for my menu, and I now want to show notifications in a dropdown inspired by the system here on Stackoverflow. To acplish this I want to call the api when clicking the dropdown, for which I need to "catch" the dropdown event. According to the docs, I need to catch the event like so:
$('#myDropdown').on('show.bs.dropdown', function () {
// do something…
})
So I've got a working fiddle here, but when I use the exact same code in my own html (see below) it doesn't work anymore. Does anybody know what's wrong with the code below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!-- jQuery -->
<script src="http://code.jquery./jquery-1.11.1.min.js"></script>
<!-- Bootstrap -->
<link href="http://maxcdn.bootstrapcdn./bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
<script src="http://maxcdn.bootstrapcdn./bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript">
$('#jajaja').on('show.bs.dropdown', function () {
alert('Come on lets show the dropdown!!');
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/">My Website</a>
</div>
<div id="navbar" class="navbar-collapse">
<ul class="nav navbar-nav navbar-right" id="jajaja">
<li class="dropdown">
<a href="" id="notifications-header" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Notifications <span class="caret"></span></a>
<ul id="notifications" class="dropdown-menu" role="menu">
<li><span class="glyphicon glyphicon-refresh text-center" aria-hidden="true"></span></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Share
Improve this question
asked Jan 30, 2015 at 14:35
kramer65kramer65
54.1k133 gold badges331 silver badges526 bronze badges
1
-
1
perhaps add a jQuery document ready around your JavaScript, e.g.
$(function() { ...your code ... }
. The#jajaja
element isn't in the dom yet when your JavaScript is executed. – ckuijjer Commented Jan 30, 2015 at 14:39
1 Answer
Reset to default 5Why it does work in the jsfiddle?
This is because you load the javascript code using onload. See the onload selection in the dropdown left.
Why doesn't it work on my website?
Because you place the code in the head and you don't wait till the dom is ready.
How can I fix this?
Check if the dom is ready. Can be done like so:
$(document).ready(function() {
$('#jajaja').on('show.bs.dropdown', function () {
alert('Come on lets show the dropdown!!');
});
});
Or in short:
$(function() {
$('#jajaja').on('show.bs.dropdown', function () {
alert('Come on lets show the dropdown!!');
});
});
Other good practice is placing all javascript just before closing the body </body>
.
本文标签: javascriptHow to catch Bootstrap navbar dropdown eventStack Overflow
版权声明:本文标题:javascript - How to catch Bootstrap navbar dropdown event? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744944437a2633687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论