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>
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744944437a4602525.html
评论列表(0条)