Javascript jQuery Mobile-如何根据上一页更改页面行为

Javascript jQuery Mobile-如何根据上一页更改页面行为,javascript,jquery,jquery-mobile,Javascript,Jquery,Jquery Mobile,我的应用程序中有三个页面:#main、#new和#existing 每次加载“new”时,我都希望它检测它是来自“menu”还是“existing”。根据这些信息,它要么什么也不做,要么填充它的表单 如何才能在上一页的正确命令和正确处理DOM方面实现这一点?根据您的要求,这些页面需要是DOM元素。只需在javascript中使用该变量,并在每个步骤中进行检查 创建一个新变量,如lastPage 每次单击时,我都喜欢使用类似HTML5数据属性的data foo 根据导航更改最后一页 然后做你真正

我的应用程序中有三个页面:#main、#new和#existing

每次加载“new”时,我都希望它检测它是来自“menu”还是“existing”。根据这些信息,它要么什么也不做,要么填充它的表单

如何才能在上一页的正确命令和正确处理DOM方面实现这一点?

根据您的要求,这些页面需要是DOM元素。只需在javascript中使用该变量,并在每个步骤中进行检查

  • 创建一个新变量,如lastPage
  • 每次单击时,我都喜欢使用类似HTML5数据属性的data foo
  • 根据导航更改最后一页
  • 然后做你真正想做的事

     var lastPage = '';
    
     /* This code from jQuery Mobile, link is below the code. */
    
     // Define a click binding for all anchors in the page
     $( "a" ).on( "click", function( event ){
         // Prevent the usual navigation behavior
         event.preventDefault();
         // Alter the url according to the anchor's href attribute, and
         // store the data-foo attribute information with the url
     $.mobile.navigate( this.attr( "href" ), {
         lastPage: this.attr("data-foo") // store data here
     });
     // Hypothetical content alteration based on the url. E.g, make
     // an AJAX request for JSON data and render a template into the page.
         alterContent( this.attr("href") );
     });
    
HTML示例

<a href="foo.bar" data-foo="main">Main Page</a>
<a href="foo.bar" data-foo="new">New Page</a>
<a href="foo.bar" data-foo="existing">Existing Page</a>

你可以制作任何你想要的数据。只有数据是重要的,之后你可以使用一切。它只是一个HTML5DOM属性

编辑:

我建议您查看导航页面。之后你可以更好地理解这个概念。 代码源:

根据您的要求,这些页面需要是dom元素。只需在javascript中使用该变量,并在每个步骤中进行检查

  • 创建一个新变量,如lastPage
  • 每次单击时,我都喜欢使用类似HTML5数据属性的data foo
  • 根据导航更改最后一页
  • 然后做你真正想做的事

     var lastPage = '';
    
     /* This code from jQuery Mobile, link is below the code. */
    
     // Define a click binding for all anchors in the page
     $( "a" ).on( "click", function( event ){
         // Prevent the usual navigation behavior
         event.preventDefault();
         // Alter the url according to the anchor's href attribute, and
         // store the data-foo attribute information with the url
     $.mobile.navigate( this.attr( "href" ), {
         lastPage: this.attr("data-foo") // store data here
     });
     // Hypothetical content alteration based on the url. E.g, make
     // an AJAX request for JSON data and render a template into the page.
         alterContent( this.attr("href") );
     });
    
HTML示例

<a href="foo.bar" data-foo="main">Main Page</a>
<a href="foo.bar" data-foo="new">New Page</a>
<a href="foo.bar" data-foo="existing">Existing Page</a>

你可以制作任何你想要的数据。只有数据是重要的,之后你可以使用一切。它只是一个HTML5DOM属性

编辑:

我建议您查看导航页面。之后你可以更好地理解这个概念。
代码源:

利用jQuery移动页面事件,例如pagebeforehidepageshow

在导航离开之前存储当前页面的
id

var prevPage;

$(document).on('pagebeforehide', function () {
  prevPage = $.mobile.activePage[0].id;
});
当目标页面处于活动状态时,检索存储的上一页
id

$(document).on('pageshow', function () {
  $(this).find('.destination').text('Previous Page: ' + prevPage);
});

利用jQuery移动页面事件,例如
pagebeforehide
pageshow

在导航离开之前存储当前页面的
id

var prevPage;

$(document).on('pagebeforehide', function () {
  prevPage = $.mobile.activePage[0].id;
});
当目标页面处于活动状态时,检索存储的上一页
id

$(document).on('pageshow', function () {
  $(this).find('.destination').text('Previous Page: ' + prevPage);
});

#main
是一个页面(单独的文件)还是一个元素/容器?需要一些代码来使用。请创建一个JSFIDLE#main是一个用HTML定义的页面,它们都是。我将立即创建一个JSFIDLE。给你。“保存”按钮不工作,因为我正在尝试执行的条件。如果删除If statmenent,您可以保存并加载“patients”。实现这一点的方法有很多,例如
$(document).on('pagehide',function(){alert($(this).attr('id');})
#main
一个页面(单独的文件)还是一个元素/容器?需要一些代码来使用。请创建一个JSFIDLE#main是一个用HTML定义的页面,它们都是。我将立即创建一个JSFIDLE。给你。“保存”按钮不工作,因为我正在尝试执行的条件。如果删除If statmenent,您可以保存并加载“patients”。实现这一点的方法有很多,例如
$(document).on('pagehide',function(){alert($(this).attr('id');})