我有2頁使用相同的模板。唯一不同的是data-role="content"
是不同的。我正在使用$.mobile.changePage('page2.php');
,但我腦海中的all.js
文件沒有綁定到在page2
中加載的元素。jquery .on()不綁定到jquerymobile的新元素changePage
腳本標記爲「all.js」是在兩個頁面的頭,從我已閱讀,只有data-role="page"
從第二頁加載。這很好,因爲.on()
綁定應該綁定到新元素,但事實並非如此。如果我特別刷新page2.php
然後all.js
文件工作,但如果我使用changePage
它不會。
爲什麼不是從changePage
綁定到all.js
.on()
選擇器拉入新元素?
HTML兩種頁面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<title>Title</title>
<link rel="stylesheet" href="css/themes/default/jquery.mobile.theme-1.2.0.min.css">
<link rel="stylesheet" href="css/themes/default/jquery.mobile.structure-1.2.0.min.css">
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/pages/all.js"></script>
</head>
<body>
<div data-role="page" data-theme="d">
<div data-id="head" data-role="header" data-position="fixed" data-theme="b">
<h1>Name</h1>
<a href="#" id="headingsActionButton" class="ui-btn-right headingsActionButton hidden" data-role="button" data-inline="true" data-icon="check">Save</a>
</div>
<div id="mainContent" data-role="content">
{PageContent}
</div>
<div align="center" data-id="foot" data-role="footer" data-position="fixed" data-inset="false" data-theme="c">
Powered by
</div>
</div>
</body>
</html>
all.js
$(function() {
/**
* Prevent the header and footer from hiding on a non element tap.
*/
$("[data-role=header], [data-role=footer]").fixedtoolbar({ tapToggle: false });
/**
* Scroll to the top of the collapsible heading
*/
$('.ui-collapsible-heading').on('click', function(e) {
window.scrollTo(0, $(this).offset().top - $('[data-role=header]').height());
$('.headingsActionButton').addClass('hidden');
var attr = $(this).parent().attr('data-action-id');
if (typeof attr !== 'undefined' && attr !== false) {
if (!$(this).parent().hasClass('ui-collapsible-collapsed')) {
console.log('expanded');
$('.headingsActionButton').attr('data-action-id', $(this).parent().attr('data-action-id'));
$('.headingsActionButton .ui-btn-text').text($(this).parent().attr('data-action-text'));
$('.headingsActionButton').toggleClass('hidden');
} else {
console.log('collapsed');
}
}
});
});
我想這是'$(文件)。在( '點擊', '的.ui-可摺疊標題')'和沒有工作。 – Bot
另請注意,如果我在changePage後手動刷新頁面,這可以工作。 – Bot
哦,同樣,刪除'$(function(){',在jQuery mobile中使用文檔並不是一個好主意,因爲它只在第一頁被調用,我不確定這對fixedToolbar意味着什麼代碼。 –