问题
I have the following demo HTML file that I am tying to use the JQuery-Mobile collapsible expand/collapse events and I am not able to get the JavaScript event to fire. I am basing this off the JSFiddle example here: http://jsfiddle.net/6txWy/
I feel like I am just overlooking something simple but here is my HTML file:
<html>
<head>
<title>References</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<link rel="stylesheet" href="css/main.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
<script type="text/javascript">
$('#my-collaspible').bind('expand', function()
{
alert('Expanded');
}).bind('collapse', function () {
alert('Collapsed');
});
</script>
</head>
<body>
<div data-role="page">
<div data-role="content">
<div data-role="collapsible" id="my-collaspible">
<h3>My Title</h3>
<p>My Body</p>
</div>
</div>
</div>
</body>
</html>
The collapse and expand work but the alerts are never fired.
Thanks! Flea
回答1:
Add a PageInit call to make sure the page is loaded before binding the events
Example:
- http://jsfiddle.net/UT7kQ/
Code:
$('#home').live('pageinit', function(event) {
$('#my-collaspible').bind('expand', function() {
alert('Expanded');
}).bind('collapse', function() {
alert('Collapsed');
});
});
来源:https://stackoverflow.com/questions/10819817/unable-to-get-jquery-mobile-collapsible-expand-collapse-event-to-execute