How exactly does $.mobile.activePage property work?

扶醉桌前 提交于 2020-01-01 04:39:24

问题


I am trying something as follows,

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

   if ($.mobile.activePage == $('#page_spots')) { console.log ('Bingo!'); }
});

Being #page_spots a div with the attribute data-role set to page. In the example above, when the active page is #page_spots I want to log 'Bingo!' in the console.

I am a complete newbie to jQM and I don't know if this should be the right way or not.

Thank you in advance and apologies for my english.


回答1:


You can get the active page's ID from $.mobile.activePage and compare it to a string rather than trying to compare to a jQuery object:

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

   if ($.mobile.activePage.attr('id') == 'page_spots') { console.log ('Bingo!'); }
});

Here is a demo: http://jsfiddle.net/E6YuA/

$.mobile.activePage is nice to have because it is always a cached object of the current data-role="page" element that you can quickly reference.

Update

I was just reading this again and you don't need to use .attr() to find the ID, you can a bit more quickly by accessing the attribute directly from the DOMElement: $.mobile.activePage[0].id



来源:https://stackoverflow.com/questions/8440452/how-exactly-does-mobile-activepage-property-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!