$ .mobile.activePage属性究竟是如何工作的?

nov*_*ato 10 jquery jquery-mobile

我正在尝试如下的事情,

$(document).bind ('pageshow', function (e, data) {
   console.log ($('#page_spots'));
   console.log ($.mobile.activePage);

   if ($.mobile.activePage == $('#page_spots')) { console.log ('Bingo!'); }
});
Run Code Online (Sandbox Code Playgroud)

作为#page_spots属性data-role设置为div的div page.在上面的示例中,当活动页面是#page_spots我要记录'宾果!'时 在控制台中.

我是jQM的完全新手,我不知道这是否应该是正确的方式.

提前谢谢你,并为我的英语道歉.

Jas*_*per 27

您可以从中获取活动页面的ID $.mobile.activePage并将其与字符串进行比较,而不是尝试与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!'); }
});
Run Code Online (Sandbox Code Playgroud)

这是一个演示:http://jsfiddle.net/E6YuA/

$.mobile.activePage很高兴,因为它总是一个data-role="page"你可以快速引用的当前元素的缓存对象.

更新

我只是再次读取它而你不需要.attr()用来查找ID,你可以通过直接从DOMElement访问属性来更快一点:$.mobile.activePage[0].id