Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jquery mobile中从下一页访问div id_Javascript_Jquery_Html_Jquery Mobile - Fatal编程技术网

Javascript 如何在jquery mobile中从下一页访问div id

Javascript 如何在jquery mobile中从下一页访问div id,javascript,jquery,html,jquery-mobile,Javascript,Jquery,Html,Jquery Mobile,page1.html <!DOCTYPE html> <html> <head> <script src="common_script.js"></script> </head> <body onload="init()"> <h1>My First Page</h1> <p id="demo">This

page1.html

<!DOCTYPE html>
<html>
    <head>
        <script src="common_script.js"></script>
    </head>
    <body onload="init()">
        <h1>My First Page</h1>
        <p id="demo">This is a paragraph.</p>
    </body>
</html> 
<!DOCTYPE html>
<html>
    <head>
        <script src="common_script.js"></script>
    </head>
    <body>
        <h1>My second Page</h1>
        <div id="nextpageId">I want to access my div</div>
    </body>
</html> 
在下面的代码中,我有两个页面,即
page1.html
page2.html
,它们包含公共JavaScript文件


我的问题是在加载
page1.html
时访问
page2.html
div#id

在javascript中使用它

$('body').attr('unique_id')
并在body标签中为页面提供唯一id

<body id='unique_id'>

您没有明确声明您正在使用jQuery Mobile,但您已经标记了您的问题,并且正在使用PhoneGap,因此我将假设您正在使用jQuery Mobile

您的页面不符合标准jQuery Mobile布局。他们缺少所需的
数据角色
s。特别是
data role=“page”
以及子
data role=“content”
声明

您的第二个页面将通过Ajax加载,第二个页面上的
标记将被忽略。实际上,只会加载嵌套的
节的内容

要提取该标记,只需访问给定ID的
,该ID应该是唯一的。您可以使用
pageinit
pageshow
事件来控制Ajax加载的计时


虽然现代手机可以做很多事情,但这是不可能的。仅仅使用普通的
id
,无法从服务器捕获页面和其中的元素。为什么要这样做?http是无状态的,如果您想保留从一个页面到另一个页面的状态,您需要使用Cookie和/或管理状态服务器端。好的,谢谢。我如何使用jquery mobile解决我的问题。登录页面是page1.html,listview页面是page2.html请更清楚地解释您的问题。您的登录页面可能会有一个“登录”按钮,其中包含一个表单,该表单包含您在服务器端验证的用户id和密码。如果登录有效,您将第2页html发送回浏览器,实际上我已经使用phonegap构建了应用程序;登录表和listvie表。当应用程序第一次加载数据库时,将创建。index.html页面第一次加载当用户单击提交按钮时,它将转到listview页面。问题是index.html是第一次加载我需要从listview页面获取id。但id没有获取。是否有任何方法可以解决我的问题…谢谢。请给我一个例子。
<body id='unique_id'>
function init() {
    alert( $('div').attr('id') == "nextpageId" )
}