問題は、document.ready
AJAXを介してDOMに取り込まれるページのコードを実行するために使用していることです。つまり、DOMに追加される各ページのイベントにバインドする場合は、またはのいずれかを使用する必要がありpageinit
ますpagecreate
。
変化する:
$(function(){
var sidebar = $('.hor-nav');
sidebar.delegate('a.inactive','click',function(){
sidebar.find('.active').toggleClass('active inactive');
$(this).toggleClass('active inactive');
});
});
に:
//run this code when a pseudo-page is added to the DOM
$(document).delegate('[data-role="page"]', 'pageinit', function(){
//only get the .hor-nav elements in this pseudo-page
var $sidebar = $(this).find('.hor-nav');
//no need to delegate here since the elements for this page exist in the DOM now
$sidebar.find('a').bind('click',function(){
$sidebar.find('.active').toggleClass('active inactive');
$(this).toggleClass('active inactive');
});
});
document
これは、DOM( )に常に存在する要素に対してイベント委任を使用しますが、.hor-nav
要素が外部ページの一部である場合、要素がDOMに常に存在するとは限りません。
イベントに依存する他のコードがあるようですがdocument.ready
、jQueryMobileWebサイトには当てはまらないはずです。このドキュメントをご覧ください:http://jquerymobile.com/demos/1.1.0/docs/api/events.html(大きな黄色の警告に注意してください)