cordova&jquery:发送到另一个静态 html 页面的值 [英] cordova&jquery:value sent to another static html page

查看:21
本文介绍了cordova&jquery:发送到另一个静态 html 页面的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CordovaJQuery 做一个 ios 网络应用程序.我创建了具有 3 行列表视图的 index.html.单击该行时,该页面将更改为另一个 page.html.JS会根据点击的行向服务器询问数据并刷新page.html.现在我的问题是实施它的最佳实践是什么?我的意思是这是常规风格,因为我对前端很陌生.目前我在 index.html 中添加了锚点:

I'm doing an ios web app using Cordova and JQuery. I create the index.html which has a 3-row list view. when the row is clicked, the page will change to another page.html. According to the clicked row, the JS will ask data from the server and refresh the page.html. Now My Question is what is the best practice to implement it? I mean which is the regular style to do it as I'm fresh to the front-end. Currently I add the anchor in index.html:

<li><a href="page.html">A Header Bars</a></li>

但是,我遇到了问题:

  1. 我不知道如何在 page.html 中获取被点击的行信息
  2. 在 page.html 中,如何发起获取数据的请求?在 page.html 中,我写:

  1. I don't know how to get the clicked row information in page.html
  2. In page.html, how I can start a request to fetch the data? In page.html, I write:

$(document).ready(function()
{
    console.log("test");
    document.write("page test");
})

但它没有被调用.

谢谢.

推荐答案

如果您想要从一个页面到另一个页面的值,您有几个选择(相同的规则适用于具有多个页面的单个 jQM html 以及围绕多个 html 构建的 jQM 项目文件):

If you want a value from one page to another you have few options (same rules apply for single jQM html with multiple page's and for jQM project built around multiple html files):

我.在第二页上使用 pagebeforeshow 并通过数据对象检索所有需要的数据.假设您有 2 个 html 文件,第一个 html 的 id 为page1",第二个 html 的 id 为page2"),例如:

I. On the second page use pagebeforeshow and retrieve all needed data through a data object. Lets say you have 2 html files, first html has an id "page1" and second one has an id "page2"), example:

$('#page2').live('pagebeforeshow', function (e, data) {
    alert(data.prevPage.find('div[data-role="content"]').attr('id'));
});

二.第二种选择是创建一个共享对象,用作数据存储:

II. Second option is to create a shared object which will be used as data storage:

var storeObject = {
    someValue : '1',
    anotherValue : '2'
}

这是一个最简单的解决方案,但它仅在 ajax 页面加载处于活动状态时有效.

This is an easiest solution but it will only work while ajax page loading is active.

三.您可以使用 changePage 传递值:

III. You can pass values with changePage:

$.mobile.changePage('page2.html', { dataUrl : "page2.html?paremeter=123", data : { 'paremeter' : '123' }, reloadPage : true, changeHash : true });

并像这样阅读它们:

$('#page2').live('pagebeforeshow', function (e, data) {
    var paremeter = $(this).data("url").split("?")[1];;
    paremeter = paremeter.replace("paremeter=","");   
    alert(paremeter);
});

更多信息

如果您想了解有关此主题的更多信息,请查看此 文章.您会通过示例找到多种解决方案.

More info

If you want to learn more about this topic take a look at this article. You will find several solutions with examples.

这篇关于cordova&amp;jquery:发送到另一个静态 html 页面的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆