【问题标题】:Jquery mobile $.mobile.changePage not loading scriptsJquery mobile $.mobile.changePage 未加载脚本
【发布时间】:2013-11-26 08:45:19
【问题描述】:

我的问题是 Jquery mobile 的 changePage(),首先让我说我知道还有很多其他关于这个问题的帖子,但我已经尝试了所有解决方案,但没有任何运气,所以这就是我现在要求帮助。

我已经构建了一个 Phonegap 项目,其中每个屏幕都是一个 .html 文件。我得到了加载整个应用程序所需的所有脚本的 index.html。然后我的 index.html 使用 $.mobile.changePage() 加载一个新页面。但是当这种情况发生时,下一页确实有任何脚本或 css 附加到它上面,为什么?

这如果我的 index.html

<!--index page used to find out if the user should see the login or welcome page-->
<!DOCTYPE html> 
<html>
<head>
<meta charset="utf-8">
<title>jQuery Mobile Web App</title>
<link href="jquery-mobile/jquery.mobile.theme-1.3.2.min.css" rel="stylesheet" type="text/css"/>
<link href="jquery-mobile/jquery.mobile.structure-1.3.2.min.css" rel="stylesheet" type="text/css"/>
<link href="jquery-mobile/jquery.mobile-1.3.2.min.css" rel="stylesheet" type="text/css"/>
<link href="jquery-mobile/jqm-icon-pack-2.0-original.css" rel="stylesheet" type="text/css"/>
<link href="jquery-mobile/jqm-icon-pack-fa.css" rel="stylesheet" type="text/css"/>
<script src="jquery-mobile/jquery-1.10.2.min.js" type="text/javascript"></script>
<!--Used to make sure theres as little delay on old devises as possible-->
<script src="buttonTapDelayJS.js" type="text/javascript"></script>
<script src="jquery-mobile/jquery.mobile-1.3.2.min.js" type="text/javascript"></script>
<script src="loginJS.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="menuJS.js" type="text/javascript"></script>
<script src="generalFunctionsJS.js" type="text/javascript"></script>
<script src="javaIndex.js" type="text/javascript"></script>
<script src="myJavaScript.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="menuJS.js" type="text/javascript"></script>
<script src="generalFunctionsJS.js" type="text/javascript"></script>
<!--Loads the css for the menu-->
<link href="menuStyle.css" rel="stylesheet" type="text/css"/>

<script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    var checkUser="admin";
    function onDeviceReady() {
        /*if the stored username is = the correctusername, it loads the welcome page if not it loads the login*/
        if(window.localStorage.getItem("user") === checkUser){
            $.mobile.changePage("welcome.html",{transition:"slide"});
            //window.open("welcome.html", "_self"); 
        } else {
            //window.open("login.html", "_self");
            $.mobile.changePage("login.html",{transition:"slide"});

        }
    }

    </script>
</head>

<body onload="onDeviceReady()"> 
</body>
</html>

这是我的登录页面

<!--Login page-->
<!DOCTYPE html> 
<html>
<body onload="loadLogin();"> 
<!--Creates all content you see on the screen-->
<div data-role="page" id="login">
    <div data-role="content">
      <ul data-role="listview">
        <div align="center" data-role="content">
        <img src="icon.png" alt="">
        </div>
        <div data-role="content">
          <label for="textinput">Indtast brugernavn</label>
          <input type="text" name="textinput" id="textinput" value=""/>
        </div>
        <div data-role="content">
            <label for="passwordinput">Indtast adgangskode</label>
            <input type="password" name="passwordinput" id="passwordinput" value=""  />
          </span>          </div>
          <div data-role="content">
        <div><a href="#" data-role="button" onClick="saveLogin();myFunction();">Login</a></div>
        </div>
        <div data-role="content">
        <div><a href="#" id="infoButton" data-role="button" data-icon="info" data-theme="b" onClick="infoPopup();"></a></div>
        </div>
      </ul>         
    </div>
</div>
</body>
</html>

如开头所述,我的问题是我的登录页面上的所有函数都不能举例说明我的 infoButton 运行 onClick="infoPopup(); 并且此函数会弹出 navigator.notification .alert("Message", null, "Info", "OK"); 但是当我按下它时它确实没有显示任何内容。

我是在加载我的脚本错误还是出了什么问题,非常感谢任何帮助?

编辑 1: 如果我将所有 index.html 复制到所有其他页面,会发生什么。我的 index.html 链接到我的 login.html 正确。在登录页面上我的 onClick="infoPopup(); dosent 工作,但我的 onClick="saveLogin();myFunction();" 有点工作。我的登录信息确实被保存但在我的 saveLogin(); 我有一个导航器.notification.alert(); 但警报并没有弹出。我知道我的所有功能都有效,因为当我使用 window.open(); 而不是 changePage 时它们都有效。我改变的原因是因为 window.open (); 我无法获得 jquery 页面转换有什么想法吗?

编辑 2: 真的不知道我做了什么,但现在可以工作了:) 感谢您的回复。 即使我已经厌倦了这对我有用的是将所有 javascripts 和 css 移动到 index.html。

【问题讨论】:

  • 在页面divdata-role=page内添加其他页面的js代码。
  • 我厌倦了链接 loginJS 包含登录功能,作为
    但是没有发送工作
  • 如果我将所有 index.html 复制到所有其他页面,会发生这种情况。我的 index.html 链接到我的 login.html 正确。在登录页面上我的 onClick="infoPopup(); dosent 工作,但我的 onClick="saveLogin();myFunction();" 有点工作。我的登录信息确实被保存但在我的 saveLogin(); 我有一个导航器.notification.alert(); 但警报并没有弹出。我知道我的所有功能都有效,因为当我使用 window.open(); 而不是 changePage 时它们都有效。我改变的原因是因为 window.open (); 我无法获得 jquery 页面转换有什么想法吗?

标签: javascript jquery-mobile mobile cordova


【解决方案1】:

jQuery Mobile 不会将整个页面拉入 dom,它会抓取第一个 data-role="page" 元素及其后代并将其拉入当前 dom。

因此不会包含文档中的任何脚本。

我通常将我网站的所有功能 JavaScript 放在索引页面上,然后当外部页面加载到 dom 中时,它们可以从已经加载的脚本中受益。

此外,您可以将 JavaScript 代码放置在 data-role="page" 元素中,当 jQuery Mobile 对页面进行 AJAX 加载时,它将被包含在内。

【讨论】:

  • 其他方式 - 一个好的系统是将所有 JS 放入包含文件中,并将其包含在站点的每个页面上。如果通过 AJAX 将页面引入 DOM,它将被忽略,但如果有人刷新您网站中的某处,则 JS 将可用
  • 我厌倦了将所有 js 链接到我的索引中,正如您在我的代码中看到的那样,但确实发送似乎正在工作。
【解决方案2】:

使用每个 html 页面中的所有脚本重复 head 部分,因为更改页面会导致页面重新加载并重新创建 head 部分。 简单的改变 -

$.mobile.changePage("welcome.html",
  {transition:"slide"}
 );

【讨论】:

  • 我厌倦了将我所有的 JS 和 css 复制到我所有页面的开头,但确实发送了工作:(
猜你喜欢
相关资源
最近更新 更多
热门标签