繁体   English   中英

如何从jQuery Mobile的下一页访问div ID

[英]How to access div id from next page in 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 is a paragraph.</p>
    </body>
</html> 

page2.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> 

common_script.js

function init() {
    alert($("#nextpageId").text());
}

在下面的代码中,我有两个页面,即page1.htmlpage2.html ,它们包含通用的JavaScript文件。

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

在你的JavaScript中使用它

$('body').attr('unique_id')

并在正文标签中为您的页面提供唯一的ID

<body id='unique_id'>

或者您也可以传递网址

或使用此代码

function init() {
    alert( $('div').attr('id') == "nextpageId" )
}

您没有明确声明您使用的是jQuery Mobile,但是您已经这样标记了您的问题并且正在使用PhoneGap,所以我假设是这样...

您的页面不符合标准的jQuery Mobile布局。 他们缺少必需的data-role 特别是data-role="page"以及子data-role="content"声明。

http://jquerymobile.com/demos/1.2.0/docs/pages/page-anatomy.html

您的第二页将通过Ajax加载,第二页上的<script>标记将被忽略。 实际上,将仅加载嵌套的<div data-role="page">部分的内容。

要拉出该标记,您可以简单地访问<div>给出其ID,并且该ID应该是唯一的。 您可以使用pageinitpageshow事件来控制Ajax加载的时间。

http://jquerymobile.com/demos/1.2.0/docs/api/events.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM