javascript 如何在 Jquery mobile 中链接内部页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11606354/
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-26 13:48:05  来源:igfitidea点击:

How to link Internal Pages in Jquery mobile?

javascriptjqueryjquery-mobile

提问by priyankirk

What to do if i want to link two internal pages on the click of a button using jquery or javascript... i can do it using <a href=#nextPage">in HTML but i want a script to do it! what i am doing right now is:

如果我想使用 jquery 或 javascript 单击按钮来链接两个内部页面该怎么办...我可以<a href=#nextPage">在 HTML 中使用它,但我想要一个脚本来完成它!我现在正在做的是:

<!-- first page-->
<div data-role="page" id="firstPage">
<div data-role="header"> 
<h1>Test page</h1>
</div>
<div data-role="content">
<p>this page will link internal pages using # </p>
<a href="#nextPage" data-role="button">change to next page</a>
</div>
</div>

<!-- second page-->
<div data-role="page" id="nextPage">
<div data-role="header"> 
<h1>Test page 2 </h1>
</div>
<div data-role="content">
<p>this page will  appear after you click the button on first page. </p>
</div>
</div>

this is doing good, but what i need is a script to link them. as in my app i need to change the page on click of button only after certain conditions met. but i dont know how to do it...

这很好,但我需要的是一个脚本来链接它们。就像在我的应用程序中一样,只有在满足某些条件后,我才需要单击按钮来更改页面。但我不知道该怎么做...

-----------Edit------------ I found out a way window.location.href="#nextPage" this is how its done. Thanks all for your effort..

-----------Edit------------ 我找到了一种方法 window.location.href="#nextPage" 这就是它的完成方式。谢谢大家的努力。。

回答by nhahtdh

You can add onclickhandler to the atag:

您可以onclicka标签添加处理程序:

JS

JS

function navigate() {
    if (condition) {
        $.mobile.changePage("#nextPage");
    }
}

HTML

HTML

<a href="#" onclick="navigate()" data-role="button">change to next page</a>

OR you can do this:

或者你可以这样做:

JS

JS

$(document).ready(function() {
    $(".nav-nextPage").click(function() {
        if (condition) {
            $.mobile.changePage("#nextPage");
        }
    });
});

HTML

HTML

<a href="#" class="nav-nextPage" data-role="button">change to next page</a>

回答by pat34515

jQuery Mobile, Anatomy of a Page

jQuery Mobile,页面剖析

Scroll down to Local, internal linked "pages"

向下滚动到本地、内部链接的“页面”