简体   繁体   English

$ .mobile.changePage无法正常工作

[英]$.mobile.changePage not working

I'm trying to use $.mobile.changePage but it doesn't work. 我正在尝试使用$.mobile.changePage但是它不起作用。 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') 我已经收到错误TypeError: 'undefined' is not an object (evaluating 'i.trigger')

I allready tried to change to " http://www.google.com " : still getting the same error 我已经准备尝试将其更改为“ http://www.google.com ”:仍然出现相同的错误

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: 将您的jquery代码封装在document.ready处理程序中,如下所示:

$(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. 因为否则您没有花时间加载DOM,因此在尝试获取它时#qu元素不存在。

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"); 在您的代码中$.mobile.changePage("#qu"); is called before the dom is initiated. 在dom启动之前被调用。 You can use many other events such as pagebeforeshow , pagecreate etc. Check the jQuery Mobile documentation for more information. 您可以使用许多其他事件,例如pagebeforeshowpagecreate等。有关更多信息,请查看jQuery Mobile 文档

Make sure you are not using $(doucment).ready(function(){}); 确保您没有使用$(doucment).ready(function(){}); as well. 也一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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