简体   繁体   中英

$.mobile.changePage not working

I'm trying to use $.mobile.changePage but it doesn't work. Here is my code

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">    
         <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css">
         <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
         <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
    </head>
    <body>
        <div data-role="page" id="p1">
            <div data-role="header"><h2>DKT</h2></div>
            <div data-role="content">
                <div id="sw1"></div>
                <div id="sw2"></div>
            </div>
            <div data-role="footer">...</div>
        </div>
        <script>
            $.mobile.changePage("#qu");
        </script>
        <div data-role="page" id="qu">
            <div data-role="header"><h2>DKT</h2></div>
            <div data-role="content">
                <fieldset class="ui-grid-a">
                    <div class="ui-block-a"><button>Nein</button></div>
                    <div class="ui-block-b"><button>Kaufen</button></div>
                </fieldset>
            </div>
        </div>
    </body>
</html>

I'm already getting the error TypeError: 'undefined' is not an object (evaluating 'i.trigger')

I allready tried to change to " http://www.google.com " : still getting the same error

Edit:

Thanks to all. But the problem was actually in another part of my code.

Enclose your jquery code in a document.ready handler, like this:

$(document).ready(function() {
   $.mobile.changePage("#qu");
});

Because otherwise you are not giving time for the DOM to be loaded and so the #qu element is not present when trying to get it.

Add Your script after

 <div data-role="page" id="qu">

OR after end of body tag

like this :

 <script>
             $.mobile.changePage("#qu");
         </script>

Change your code as below

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">    
         <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css">
         <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
         <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
    </head>
    <body>
        <div data-role="page" id="p1">
            <div data-role="header"><h2>DKT</h2></div>
            <div data-role="content">
                <div id="sw1"></div>
                <div id="sw2"></div>
            </div>
            <div data-role="footer">...</div>
        </div>
        <script>
            $('#p1').live('pageshow', function(event){
                $.mobile.changePage("#qu");            
            });
        </script>
        <div data-role="page" id="qu">
            <div data-role="header"><h2>DKT</h2></div>
            <div data-role="content">
                <fieldset class="ui-grid-a">
                    <div class="ui-block-a"><button>Nein</button></div>
                    <div class="ui-block-b"><button>Kaufen</button></div>
                </fieldset>
            </div>
        </div>
    </body>
</html>

In your code the $.mobile.changePage("#qu"); is called before the dom is initiated. You can use many other events such as pagebeforeshow , pagecreate etc. Check the jQuery Mobile documentation for more information.

Make sure you are not using $(doucment).ready(function(){}); as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM