I am attempting to trigger a click on a specific <li> on my page with jQuery Mobile on the pageinit event.

I can successfully trigger the click, however I get an error on the line:

var currentPage = $.mobile.activePage;

Error: TypeError: currentPage is undefined

What I am trying to achieve is that if a parameter of reminder is passed through in the URL then I want to trigger a click on a <li>. I'm not sure if i'm using the wrong jQM event (pageinit), but for some reason there appears to be no "active page".

$(document).on('pageinit', function (event, ui) {

    var page = $(event.target);

    page.find("li.static_nav").on('click', function () {
        var currentPage = $.mobile.activePage;
        var subheader = currentPage.find("#static_page_sub_header");
        var target = $(this).attr("data-target");

        $(this).siblings().removeClass('active');
        $(this).addClass('active').find('a').clone(true, true).appendTo(subheader.empty());

        currentPage.find(".static_panel").hide();

        return false;
    });


    if (page.find('#p_content').length !== 0) {
        var url = $.mobile.path.parseUrl(document.location.href);
        var query_string = url.search;
        if (/reminder/i.test(query_string)) {
            $('li.static_nav:eq(1)', $.mobile.activePage).trigger('click');
        }
    }

});
有帮助吗?

解决方案

Move the following code out of the pageinit event and into a pageshow event:

$(document).on('pageshow', function (event, ui) {

var page = $(event.target);

if (page.find('#p_content').length !== 0) {
    var url = $.mobile.path.parseUrl(document.location.href);
    var query_string = url.search;
    if (/reminder/i.test(query_string)) {
        $('li.static_nav:eq(1)', $.mobile.activePage).trigger('click');
    }
}

});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top