javascript 在cordova中,如何导航到另一个html页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31797654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:23:47  来源:igfitidea点击:

In cordova, how to navigate to another html page?

javascriptjqueryhtmlcordova

提问by Peilin Li

I am currently working on a Cordova project in Visual Studio. In this project, I am trying building 2 html pages, let me call them first.html and second.html.

我目前正在 Visual Studio 中处理 Cordova 项目。在这个项目中,我正在尝试构建 2 个 html 页面,让我称它们为 first.html 和 second.html。

In the first.html, I want to add a link to second.html, which allows me to navigate to second.html. I tried 2 ways.

在 first.html 中,我想添加一个到 second.html 的链接,它允许我导航到 second.html。我尝试了 2 种方法。

  1. window.location

    window.location = "second.html"
    
  2. tag

    <a href=“second.html”></a>
    
  1. 窗口位置

    window.location = "second.html"
    
  2. 标签

    <a href=“second.html”></a>
    

As a result, they both caused an error saying "Exception occurred Message: Exception: Cannot redefine property: org".

结果,他们都导致了一个错误,说“发生异常消息:异常:无法重新定义属性:组织”。

Can anyone tell me how to navigate to a new page properly?

谁能告诉我如何正确导航到新页面?

回答by Shrinivas Pai

You can navigate to another page using window.location.href. An example is shown below

您可以使用 window.location.href 导航到另一个页面。一个例子如下所示

    function(){ window.location.href = "second.html";}

回答by san

You can use the below line to navigate one page to another page.

您可以使用以下行将一个页面导航到另一个页面。

 $('#yourelement').click(function(){      
    window.location.assign('name_of_page.html');
 });

回答by yb3prod

try this it work's for me

试试这个对我有用

    <!DOCTYPE HTML>
    <html>
      <head>
        <title>My PhoneGap</title>

              <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>      
              <script type="text/javascript" charset="utf-8">

                 function onLoad()
                 {
                      document.addEventListener("deviceready", onDeviceReady, true);
                 }

                 function onDeviceReady()
                 {
                      // navigator.notification.alert("PhoneGap is working");
                 }

                 function callAnothePage()
                 {
                    window.location = "test.html";
                 }

              </script>

      </head>

      <body onload="onLoad();">
            <h1>Welcome to PhoneGap</h1>
            <h2>Edit assets/www/index.html</h2>
            <button name="buttonClick" onclick="callAnothePage()">Click Me!</button>
      </body>
</html>

回答by k_kumar

Try this:

试试这个:

 window.open("second.html"); 

window.open opens a new window/tab with the selected URL, while the mentioned method in the question redirects the current page to the selected URL.

window.open 使用选定的 URL 打开一个新窗口/选项卡,而问题中提到的方法将当前页面重定向到选定的 URL。