你的問題是,你正在使用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');
});
});
這將使用事件代表團,將永遠在DOM(document
)存在一個元素,而.hor-nav
元素不總是存在DOM如果它們是外部頁面的一部分。
它看起來像你有其他代碼取決於document.ready
事件,這不應該是一個jQuery Mobile網站的情況。看看這個文檔:http://jquerymobile.com/demos/1.1.0/docs/api/events.html(注意大的黃色警告)
你試過使用'jQuery'而不是'$'? – gcochard
不確定你的意思? – Peter
'$'是'jQuery'的別名,如果您在腳本中將$更改爲jQuery,則不應再發生衝突,如果確實是原因。 – gcochard