我正在尝试如下操作,
$(document).bind ('pageshow', function (e, data) {
console.log ($('#page_spots'));
console.log ($.mobile.activePage);
if ($.mobile.activePage == $('#page_spots')) { console.log ('Bingo!'); }
});
作为 #page_spots
一个 div,其属性 data-role
设置为 page
。在上面的示例中,当事件页面为 #page_spots
时,我想记录“Bingo!”在控制台中。
我是 jQM 的新手,我不知道这是否是正确的方法。
预先感谢您,并对我的英语表示歉意。
最佳答案
您可以从 $.mobile.activePage
获取事件页面的 ID 并将其与字符串进行比较,而不是尝试与 jQuery 对象进行比较:
$(document).bind ('pageshow', function (e, data) {
console.log ($('#page_spots'));
console.log ($.mobile.activePage);
if ($.mobile.activePage.attr('id') == 'page_spots') { console.log ('Bingo!'); }
});
这是一个演示:http://jsfiddle.net/E6YuA/
$.mobile.activePage
很高兴拥有,因为它始终是您可以快速引用的当前 data-role="page"
元素的缓存对象。
更新
我刚刚又读了一遍,您不需要使用 .attr()
来查找 ID,您可以通过直接从 DOMElement 访问属性来更快一点:$.mobile.activePage[0].id
关于jquery - $.mobile.activePage 属性到底是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8440452/