Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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
jQuery mobile,多页的pageshow事件_Jquery_Events_Jquery Mobile - Fatal编程技术网

jQuery mobile,多页的pageshow事件

jQuery mobile,多页的pageshow事件,jquery,events,jquery-mobile,Jquery,Events,Jquery Mobile,我有两个页面,test1.html和test2.html。每次当这些页面中的一个可见时,我都想执行一些代码 以下是两页: test1.html: <!DOCTYPE html> <html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title&

我有两个页面,test1.html和test2.html。每次当这些页面中的一个可见时,我都想执行一些代码

以下是两页:

test1.html:

    <!DOCTYPE html>
<html><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>test1</title>
    <link rel="stylesheet"  href="css/jquery.mobile-1.2.0.css" />
    <script src="js/jquery.js"></script>
    <script src="js/jquery.mobile-1.2.0.js"></script>
</head><body>

<div data-role="page" id="test1">

    <div data-role="content">

    <a href="test2.html" data-role="button">jump to test2</a>

    </div>

</div>

<script type="text/javascript">

$(document).on('pageshow', '#test1', function (data) {
    alert('pageshow test1');
});

</script>
</body></html>

测试1
$(文档).on('pageshow','#test1',函数(数据){
警报(“页面显示测试1”);
});
test2.html:

<!DOCTYPE html>
<html><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>test1</title>
    <link rel="stylesheet"  href="css/jquery.mobile-1.2.0.css" />
    <script src="js/jquery.js"></script>
    <script src="js/jquery.mobile-1.2.0.js"></script>
</head><body>

<div data-role="page" id="test2">

    <div data-role="content">

    <a href="test1.html" data-role="button">jump to test1</a>

    </div>

</div>

<script type="text/javascript">
$(document).on('pageshow', '#test2', function (data) {
    alert('pageshow test2');
});
</script>

</body></html>

测试1
$(文档).on('pageshow','#test2',函数(数据){
警报(“页面显示测试2”);
});
当我开始使用test1.html时,我只会收到test1的警报(每次返回时也是如此),但从来不会收到test2.html的警报

我需要的是一个显示处理程序,每个页面都不同。

可用的解决方案 解决方案1 首先,从页面
正文
中删除javascript代码,并将其放入单个js文件中。在js/jquery.mobile-1.2.0.js初始化之后,新的js文件应该放在
头中

大概是这样的:

<script src="js/jquery.js"></script>
<script src="js/jquery.mobile-1.2.0.js"></script>
<script src="js/custom.js"></script>
不管怎样,如果可能,请不要在页面内使用
标记
正文
,这样做会起作用,但同时也会导致其他问题

更多信息 如果你想了解更多关于这个问题的信息,如何解决它+示例看看我在个人博客中制作的这个。这篇文章直接讨论了这个话题,所以我希望你不要把它看作是自我推销的尝试

<div data-role="page" id="test2">
    <div data-role="content">
        <a href="test1.html" data-role="button">jump to test1</a>
    </div>
</div>
<a href="test1.html" data-role="button" rel="external">jump to test1</a>